Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

recursive lambda

23 views
Skip to first unread message

jrwats

unread,
Sep 25, 2009, 12:23:42 PM9/25/09
to
so I'll the recursive lambda's I've seen don't solve the keyword
problem. My question is it non-trivial? i.e. is the following
missing something?


(defmacro lambda-rec (name args &body body)
"Just like lambda except BODY can make recursive calls to the
lambda by calling the function NAME."
(let ((fargs
(iterate:iterate (for el in args)
(unless (member el '(&key &optional &body &rest))
(collect (if (consp el) (car el) el))))))
`(lambda ,args
(labels ((,name ,args ,@body))
(,name ,@fargs)))))

(funcall (lambda-rec fib (n &optional (a 0) (b 1))
(if (= n 0)
a
(fib (1- n) b (+ a b)))) 4)

Neil T. Dantam

unread,
Sep 25, 2009, 1:01:55 PM9/25/09
to
jrwats wrote:
> so I'll the recursive lambda's I've seen don't solve the keyword
> problem. My question is it non-trivial? i.e. is the following
> missing something?
>
>
> (defmacro lambda-rec (name args &body body)
> "Just like lambda except BODY can make recursive calls to the
> lambda by calling the function NAME."
> (let ((fargs
> (iterate:iterate (for el in args)
> (unless (member el '(&key &optional &body &rest))
> (collect (if (consp el) (car el) el))))))
> `(lambda ,args
> (labels ((,name ,args ,@body))
> (,name ,@fargs)))))

Can't you just return the labels function?

(defmacro lambda-rec (name args &body body)

`(labels ((,name ,args ,@body))
#',name))

jrwats

unread,
Sep 25, 2009, 1:28:03 PM9/25/09
to

> Can't you just return the labels function?
>
> (defmacro lambda-rec (name args &body body)
>    `(labels ((,name ,args ,@body))
>              #',name))- Hide quoted text -
>

wow - indeed that works. I just double checked and that's what PG's
does. I don't know where i was coming from...
(defmacro alambda (parms &body body)
`(labels ((self ,parms ,@body))
#'self))

I think the arnesi implementation

Vassil Nikolov

unread,
Sep 26, 2009, 12:12:02 AM9/26/09
to

On Fri, 25 Sep 2009 09:23:42 -0700 (PDT), jrwats <jrw...@gmail.com> said:
> ...

> (defmacro lambda-rec (name args &body body)
> "Just like lambda except BODY can make recursive calls to the
> lambda by calling the function NAME."
> (let ((fargs
> (iterate:iterate (for el in args)
> (unless (member el '(&key &optional &body &rest))
> (collect (if (consp el) (car el) el))))))
> `(lambda ,args
> (labels ((,name ,args ,@body))
> (,name ,@fargs)))))

What to do in this particular case has already been posted, but in
any case the usual simple solution to the "keyword problem", using
the above example, is along the lines of

(defmacro named-lambda (name lambda-list &body body)
`(lambda (&rest args)
(labels ((,name ,lambda-list ,@body))
(apply #',name args))))

(modulo a gensym for ARGS, of course).

---Vassil.


--
"Even when the muse is posting on Usenet, Alexander Sergeevich?"

0 new messages