[Solved] Scheme Rswap function [closed]


Try this:

(define (rswap lst)

  ;; Create a helper function to do the recursive work.
  (define (helper in out)

    ;; If the input is not a list, simply return it.
    ;; There is nothing to be done to rswap it.
    (if (not (list? in))
      in

      ;; If in is an empty list, simply return the out.
      (if (null? in)
        out

        ;; If in is a list with only one item, append
        ;; the result of calling rswap on the item to 
        ;; out and return it.
        (if (null? (cdr in))
          (append out (list (rswap (car in))))

          ;; This is where the recursion continues.
          ;; Take two items off in before the next call.
          ;; rswap the two items and add them to out.
          (helper
            (cddr in)
            (append out (list (rswap (cadr in)) (rswap (car in)))))))))

  (helper lst '()))

solved Scheme Rswap function [closed]