knowledge-database (beta)

Current group: comp.lang.lisp

My Patented Lambda Substitute

My Patented Lambda Substitute  
David Steuber
From:David Steuber
Subject:My Patented Lambda Substitute
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
   

Copyright © 2006 knowledge-database   -   All rights reserved