Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme: when are expressions in let evaluated?

Tags:

let

scheme

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.

like image 654
user782220 Avatar asked Nov 19 '25 20:11

user782220


1 Answers

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.

like image 65
Óscar López Avatar answered Nov 21 '25 09:11

Óscar López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!