1 day ago
my attempt to do the exercises in sicp.
Wednesday, July 16, 2008
sicp exercise 2.18
; 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 NULL (list))
(define (reverse mylist)
(define reverse_impl (lambda (items result)
(if (null? items) result
(reverse_impl (cdr items) (cons (car items) result)))))
(reverse_impl mylist NULL))
(display (reverse (list 1 2 3 4 5)))(newline)
(display (reverse (list 1 2 3 4 5 (list 10 9 8))))(newline)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment