Implementation of reverse

Reverse makes a copy of the top level of a list, backwards. The original list is unchanged.

(reverse '(a b c))  = (c b a)

Recursive implementation of reverse using an auxiliary function:

(define (reverse1 lst) (reverse2 lst '()) )

(define (reverse2 lst answer) (if (pair? lst) (reverse2 (cdr lst) (cons (car lst) answer) ) answer ) )

Iterative implementation of reverse:

(define (reverse3 lst)
  (do ((answer '() ))
      ((not (pair? lst)) answer)
    (set! answer (cons (car lst) answer))
    (set! lst (cdr lst)) ) )

Contents    Page-10    Prev    Next    Page+10    Index