let

Q&A's, tips, howto's
Locked
eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

let

Post by eddier »

Would it be difficult to fix let so that I could reference other local variables like

Code: Select all

(let (x (car L) y (car x) ...)
This would reduce the amount of nested lets like

Code: Select all

(let (x (car L))
  (let (y (car x))
  ...
What do you think about this one?

Eddie

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

do you mean let* (or letrec) ?

(set 'x 10)

(let ((x 3) (y (+ x 4)) (print y)) => 14

versus:

(letrec ((x 3) (y (+ x 4)) (print y)) => 7

Lutz

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

I would use the (letrec more. What is the difference between let* and let?

Eddie

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

'let*' and 'letrec' are the same thing, they are just differently named in different LISPs. I implemented is yesterday as 'letr', I thought 'letrec' is too long to type for a function which will be used by some people quite often so 'letr' it will be in version 8.2.6

Lutz

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

Thanks Lutz :)

Eddie

Qrczak
Posts: 12
Joined: Fri Oct 01, 2004 8:46 am
Location: Warszawa

Post by Qrczak »

Lutz wrote:'let*' and 'letrec' are the same thing, they are just differently named in different LISPs.
Scheme has both let* and letrec, and they are not the same thing.

let* allows each expression to refer to variables defined above it. It guarantees that expressions will be evaluated in order. Common Lisp has the same let*.

letrec allows each expression to refer to any variable in the group, but all references must be made inside function closures - it must be possible to evaluate all expressions to values before binding any variable. Expressions may be evaluated in arbitrary order.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

thanks for the clarification

Lutz

Locked