Exercise 2.18: 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)
(define (reverse-rec ls)
(if (null? ls)
'()
(append (reverse-rec (cdr ls)) (list (car ls)))))
(define (reverse ls)
(define (reverse-iter ol nl)
(if (null? ol)
nl
(reverse-iter (cdr ol) (cons (car ol) nl))))
(reverse-iter ls '()))
> (reverse-rec (list 1 2 3 4))
(4 3 2 1)
> (reverse (list 1 2 3 4))
(4 3 2 1)