This definition is executed:
;; Like Scheme reverse (define (rev lst) (if (null? lst) '() (append (rev (cdr lst)) (list (car lst)))))
Then this expression is typed into the Scheme interpreter and evaluated. It returns the result shown:
> (rev '(x y z)) (z y x)
The following questions all concern what happens when the
expression (rev '(x y z))
is evaluated. It may be helpful to imagine what you would see if you traced the
execution of rev
and append
.
rev
called?
lst
bound to when rev
is called the first time?
(cdr lst)
when rev
is called the
first time?
(rev (cdr lst))
when rev
is called the
first time?
(car lst)
when rev
is called the
first time?
(list (car lst))
when rev
is called the
first time?
(append (rev (cdr lst)) (list (car lst)))))
when rev
is called the first time?