I am busy with Structure and Interpretation of Computer Programs exercise 2.18. Here we have to define a procedure reverse to reverse a list. It should do the following:
(reverse (list 1 4 9 16 25))
;; => (25 16 9 4 1)
I came up with the following definition:
(define (reverse list)
(if (null? list)
list
(cons (reverse (cdr list)) (car list))))
;; => (mcons (mcons (mcons (mcons (mcons '() 25) 16) 9) 4) 1).
Then in a solution In found something similar as follows:
(define (reverse items)
(if (null? (cdr items))
items
(append (reverse (cdr items))
(cons (car items) nil))))
;; => (mcons 25 (mcons 16 (mcons 9 (mcons 4 (mcons 1 '()))))).
There's a difference between append
and cons
here where I cannot put my finger on.
My question: what is the difference and why are the results not displayed as (25 16 9 4 1)
?