On page 66 of "The Seasoned Schemer" it says that (let ...)
is an abbreviation for :
(let ((x1 a1) ... (xn an)) b ...) = ((lambda (x1 ... xn) b ...) a1 ... an)
Its used on for example on page 70:
(define depth*
(lambda (l)
(let ((a (add1 (depth* (car l))))
(d (depth* (cdr l))))
(cond
((null? l) 1)
((atom? (car l)) d)
(else (cond
((> d a) d)
(else a)))))))
But that above definition of lambda
would suggest that (add1 (depth* (car l))
and (depth* (cdr l))
are evaluated and passed into the lambda represented by (lambda (x1 ... xn) b ...)
. But this would mean that the list l
, which could potentially be empty, would be passed to car
and cdr
before the null check in (null? l) 1)
is ever done.
You're right in stating that (car l)
and (cdr l)
will get executed before testing if l
is null, therefore raising an error if l
is indeed null
. Keep reading the book, in the following two pages this is explained, and a correct version of depth*
is shown.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments