3 ms·
Smalltalk blocks are anonymous functions sure, but the evaluation is delayed - you have to pass the message #value (and its variants) to it. This not the case i
by i_feel_great 8y ago
Smalltalk blocks are anonymous functions sure, but the evaluation is delayed - you have to pass the message #value (and its variants) to it. This not the case in Scheme and maybe Lisps, where evaluation is eager.
Passing Smalltalk blocks and Scheme/Lisp lambdas into functions have different behaviours.
- lispm 8y agoThat doesn't matter. One can write IF as a function taking functions as THEN and ELSE forms: CL-USER 9 > (defun my-if (test then else) (if test (funcall then) (funcall else))) MY-IF CL-USER 10 > (let ((a 10)) (my-if (> a 5) (lambda () (print 'hello)) (lambda () (print 'world))) 'done) HELLO DONE The lambdas you see above serve the same purpose as blocks in Smalltalk.
- skybrian 8y agoIt works, but the syntax isn't nice enough to use as-is, so in practice you need a macro. The innovation here is using better syntax for anonymous functions to make such things practical in languages without macros.
- lispm 8y agoIf you think brackets are the core innovation of Smalltalk to make lambdas practical, then Lisp has no problem providing it, too: CL-USER 19 > (defun bracket-reader (stream char) (declare (ignore char)) `(lambda () ,@(read-delimited-list #\] stream t))) BRACKET-READER CL-USER 20 > (set-macro-character #\[ #'bracket-reader) T CL-USER 21 > (let ((a 10)) (my-if (> a 5) [(print 'hello)] [(print 'world)]) 'done) HELLO DONE With a bit more effort, we could also parse a parameter list. But Lisp does not go the route of making IF a function, because it typically provides three different types of language expressions and IF then is a special operator: 1) function calls 2) macro forms 3) a small set of special operators, which are implemented as built-in functionality. One of them is a core conditional operator. More complex conditional operators then are implemented as macros, which expand eventually into the core conditional operator. The interpreter and compiler will have to specially recognize and implement these special operators. See IF: http://clhs.lisp.se/Body/s_if.htm http://clhs.lisp.se/Body/s_if.htm The usual Lisp view is that [(print 'hello)] is no useful improvement over (lambda () (print 'hello)) Some disagree, but most of the time the pattern (<operator> <expr0> ... <exprn>) is preferred in Lisp over <fancy-character> <expr0> ... <exprn> <another-fancy-character>
- skybrian 8y agoOkay, but if you have macros, you might as well use them. The idea is more about exploring what can be done when designing a less powerful language, without macros.
- lispm 8y ago> The idea is more about exploring what can be done when designing a less powerful language, without macros. Lot's of things. But the language won't be able to easily compute code at compile time, which a main purpose of macros. Lisp has a simple data format for source code and thus code transformations are relatively simple to do and even integrated into the language.