what is the difference between let and letex

Started by jopython, November 16, 2012, 05:39:53 PM

Previous topic - Next topic

jopython

Both look the same and behave the same:



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

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

5
>

rickyboy

#1
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/newlisp_manual.html#letex">//http://www.newlisp.org/downloads/newlisp_manual.html#letex.
(λx. x x) (λx. x x)

cormullion

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

jopython

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

> (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?