I started learning Common Lisp recently, and (just for fun) decided to rename the lambda macro.
My attempt was this:
> (defmacro λ (args &body body) `(lambda ,args ,@body))
It seems to expand correctly when by itself:
> (macroexpand-1 '(λ (x) (* x x)))
(LAMBDA (X) (* X X))
But when it’s nested inside an expression, execution fails:
> ((λ (x) (* x x)) 2)
(Λ (X) (* X X)) is not a function name; try using a symbol instead
I am probably missing something obvious about macro expansion, but couldn’t find out what it is.
Maybe you can help me out?
edit:
It does work with lambda:
> ((lambda (x) (* x x)) 2)
4
edit 2:
One way to make it work (as suggested by Rainer):
> (set-macro-character #\λ (lambda (stream char) (quote lambda)))
(tested in Clozure CL)
In Common Lisp
LAMBDAis two different things: a macro and a symbol which can be used in a LAMBDA expression.The LAMBDA expression:
shorter written as
An applied lambda expression is also valid:
Above both forms are part of the core syntax of Common Lisp.
Late in the definition of Common Lisp a macro called
LAMBDAhas been added. Confusingly enough, but with good intentions. 😉 It is documented as Macro LAMBDA.expands into
It makes Common Lisp code look slightly more like Scheme code and then it is not necessary to write
With the
LAMBDAmacro we can writeYour example fails because
is not valid Common Lisp syntax.
Common Lisp expects either
(function args...)((lambda (arglist ...) body) args...)(macro-name forms...)FUNCTION,LET, …defined in the list of special operators in Common Lisp
As you can see a syntax of
is not a part of Common Lisp.
It is possible to read the character
λasLAMBDA:Example: