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)
function
in the expansion oflambda
that is a problem, it's the hard-wired behavior of function applications withlambda
. So in your explanation I'd change: "An applied lambda expression is also valid" into "An appliedlambda
expression is also valid (only forlambda
and not for macros that expand to it)". – Raines