My Patented Lambda Substitute

From: David Steuber (david_at_david-steuber.com)
Date: 01/24/05


Date: 23 Jan 2005 23:14:44 -0500

OK, I haven't actually applied for a patent...

I was looking for a way to understand lambda better. After looking
through the CLHS and still puzzling a bit over the (lambda ...) =>
#'(lambda ...) expansion, I came up with this:

CL-USER> (defmacro my-lambda (args &body forms)
           (let ((anon (gensym)))
             `(flet ((,anon ,args ,@forms)) (function ,anon))))
MY-LAMBDA
CL-USER> (mapcar (my-lambda (x) (* 2 x)) '(1 2 3 4 5))
(2 4 6 8 10)
CL-USER> (mapcar (my-lambda (x) (* x x)) '(1 2 3 4 5))
(1 4 9 16 25)
CL-USER> (mapcar #'(my-lambda (x) (* x x)) '(1 2 3 4 5))
; Evaluation aborted

The key wasn't so much lambda the macro vs lambda the symbol. It
really turned out to be the fact that function is a special form.
When function is passed a list who's car is lambda, it returns an
anonymous function that takes arguments matching the lambda-list.

The real lesson is that I don't need lambda to make an anonymous
function or to create a closure but I do need function. Lambda
expands into function. To me, lambda used to look like a recursive
macro.

How's my understanding?

-- 
An ideal world is left as an excercise to the reader.
   --- Paul Graham, On Lisp 8.1


Relevant Pages

  • anaphoric lambda
    ... I'm trying to improve upon Graham's alambda macro (because who really ... ;; Anaphoric lambda. ... (defmacro jalambda (parms &body body) ...
    (comp.lang.lisp)
  • Re: Macros in the car of an application
    ... > try to write a macro that would allow things like that. ... > a macro I called flambda. ... Note that your macro could assume that all of the lambda variables are ... symbols by duplicating the value bindings into function bindings. ...
    (comp.lang.lisp)
  • Re: Paul Grahams Arc is released today... what is the long term impact?
    ... unfortunate choice for a *very prominent* keyword. ... macro just to change the name of something so ubiquitous smells of a bad ... even "lambda calculus" has the fussy sound of having come from the ... the "telling youself stories" about your code, ...
    (comp.lang.lisp)
  • Re: Forms in functional position: asking the implementors (Re: Is it possible to implement ,@ if it
    ... named F. So you'd need to perform the evaluation in a special way, ... not otherwise taken up by a macro, special operator, or lambda expression. ...
    (comp.lang.lisp)
  • Re: LISP
    ... In Lisp2, one can write the Y operator as ... I also can see differences between APL and the Lisp-1 ... a lambda macro which allows people to omit the #' in front of a lambda, ... but still I see people using that reader macro. ...
    (comp.lang.lisp)