Page 1 of 1

what is the difference between let and letex

Posted: Sat Nov 17, 2012 1:39 am
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
> 

Re: what is the difference between let and letex

Posted: Sat Nov 17, 2012 3:08 am
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.

Re: what is the difference between let and letex

Posted: Sat Nov 17, 2012 8:47 am
by cormullion
Ricky, perhaps you should start writing that "Advanced newLISP" book...!?

Re: what is the difference between let and letex

Posted: Sat Nov 17, 2012 1:34 pm
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?