SICP Exercise 2.18

Question

Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order:

(reverse (list 1 4 9 16 25))
(25 16 9 4 1)

Answer

(define (reverse l)
  (if (null? l)
      l
      (append (reverse (cdr l)) (list (car l)))))

(reverse (list 1 4 9 16 25))

Results:

(25 16 9 4 1)