what is the difference between let and letex

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

what is the difference between let and letex

Post by jopython »

Both look the same and behave the same:

Code: Select all

> 
(letex ((x 2) (y 3))
   (+ x y))

5
> 
(let ((x 2) (y 3))
    (+ x y))

5
> 

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: what is the difference between let and letex

Post by rickyboy »

In the letex case, when eval is ready to evaluate the (+ x y) form, all it "sees" is (+ 2 3) because letex has already macroexpanded the (+ x y) form.

In the let case, when eval is ready to evaluate the (+ x y) form, all it "sees" is (+ x y), and then it must evaluate x and y based on the current (dynamic) bindings of x and y to find the respective values.

It may sound strange, but there are times when you want to expand a form before eval sees it. Sort of like a double-eval, but in the first round (expansion with letex) you get to choose what parts of the target form get evaluated (expanded), and then eval looks at the expanded form (pre-processed, if you will) and proceeds to do its job on that.

If I wasn't clear on this, BTW, (1) it's my fault and (2) there is good news in that Lutz has written a nice example of when you might use letex. See his example make-adder function at
http://www.newlisp.org/downloads/newlis ... html#letex.
(λx. x x) (λx. x x)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: what is the difference between let and letex

Post by cormullion »

Ricky, perhaps you should start writing that "Advanced newLISP" book...!?

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: what is the difference between let and letex

Post by jopython »

Rickyboy, thank you for the explanation. I get it now.

Code: Select all

> (letex (x 1 y 2) '(x y))
(1 2)
> (let (x 1 y 2) '(x y))
(x y)
> 
I have to incorporate this to the way I think.
Can 'letex' be compared to 'C' macros?

Locked