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

  • 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: Macro and eval questions
    ... lambda takes one argument. ... macro, because that's what are hygienic macros for - to separate macro ... identifiers, reintroduce identifiers that were lexically visible at ... the point of the macro definition, and check if two identifiers are ...
    (comp.lang.scheme)
  • Re: # and lambda
    ... Since the use of this macro obscures the symmetry between ... lambda expressions and symbolic function names (where you ... with both bindings set to the same object. ... evaluation positions, so you need to be able to write ...
    (comp.lang.lisp)