5 ms·
Since Python doesn't support multi-line lambdas, how do they support the analog in LISP? It's almost necessary for any dialect of LISP to support lambdas (i.e.
by winter_blue 9y ago
Since Python doesn't support multi-line lambdas, how do they support the analog in LISP? It's almost necessary for any dialect of LISP to support lambdas (i.e. functions) that contain a LISP 'do' [1] which lets you group statements. Do they chain together expressions/statements with continuations or something?
[1] http://www.lispworks.com/documentation/lw60/CLHS/Body/m_do_do.htm http://www.lispworks.com/documentation/lw60/CLHS/Body/m_do_d...
- gkya 9y agoIIRC in a talk the author said that it just generated a function with a made-up unique name, like an internal def in another function.
- serhei 9y agoThat's pretty similar to how languages like Scala with higher-order features translate down to the JVM bytecode (which is subject to certain restrictions).
- kazinator 9y agoIn general, if some target language entity exists in a named form only, then the corresponding anonymity of those entities in the source language must be simulated via gensyms. For instance, the branch targets in a while loop are anonymous, right? But in the target language, you must have a named label for the instruction. Solution: machine-generated label.
- joejev 9y agoPython lambdas are just syntactic sugar for creating a function object. The ast can just build a named function and use it in the needed expression.
- kazinator 9y agoHowever, that approach seems rather backwards compared to having an anonymous function primitive which is used as the basis for the lambda one-line-only sugar, as well as named functions (which simply establish a binding between a name and the anonymous thing).
- aoeusnth1 9y agoWhy should the programmmer care about whether the underlying function is named or not?
- jwdunne 9y agoI imagine some kind of analogue to gensym would work there. The name can be throw away and guaranteed unique in scope. In fact, a lot of SICP examples have you define a function within a function with a name like "iter" for TCO recursion. It'd work like that. Edit: seems like gyka below confirms that's how it works. I should have read on!
- kbp 9y ago> It's almost necessary for any dialect of LISP to support lambdas (i.e. functions) that contain a LISP 'do' [1] which lets you group statements. > [1] http://www.lispworks.com/documentation/lw60/CLHS/Body/m_do_do.htm http://www.lispworks.com/documentation/lw60/CLHS/Body/m_do_d... 'do' in most Lisps and Schemes is essentially an extended for loop, not a blocking construct. Of course, you could write (do ((once nil t)) ; Scheme: (do ((once #f #t)) (once) (thing-1) (thing-2) ...) or something, but that's silly. The blocking construct is in CL called PROGN and in Scheme called begin. Lambdas in CL get what's called an "implicit PROGN" around their bodies, so you can put however many forms you want inside and the value of the last is returned. It works the same in modern Scheme lambdas. Calling PROGN/begin "do" is solely a Clojureism as far as I know.