Greeting.
I want to define an anonymous procedure (lambda), and that procedure
is a recursive one.
For example,
(let ((foo (lambda lst)
(if lst
(progn (print (car lst))
(foo (cdr lst)))))))
(foo '(1 2)))
But, of course, it didn't work.
I just remembered that LISP namespace for variables and procedures are
different (opposite to Scheme), if I'm correct.
In Scheme, I would do:
(letrec ((foo (lambda (lst)
(if (not (null? lst))
(begin (display (car lst))
(foo (cdr lst)))))))
(func '(1 2 3)))
My question is, how can I get same effect on Lisp?
I just read some docs, but I couldn't find any example that using
let-like form with lambda expression.
Thanks in advance. ;-)
In article <····························@posting.google.com>,
Seong-Kook Shin <·····@nownuri.net> wrote:
> I just remembered that LISP namespace for variables and procedures are
> different (opposite to Scheme), if I'm correct.
>
> In Scheme, I would do:
>
> (letrec ((foo (lambda (lst)
> (if (not (null? lst))
> (begin (display (car lst))
> (foo (cdr lst)))))))
> (func '(1 2 3)))
>
> My question is, how can I get same effect on Lisp?
You want LABELS.
http://www.lispworks.com/reference/HyperSpec/Body/s_flet_.htm
LABELS is like FLET (which you may have seen), but the function
bindings are present in the definition itself, allowing recursion.
Your example in CL would be (I assume you meant (foo '(1 2 3)) at
the end):
(labels ((foo (list)
(if (not (null list))
(progn (print (car list))
(foo (cdr list))))))
(foo '(1 2 3)))
Of course in CL
(dolist (element '(1 2 3))
(print element))
is easier and (arguably) more in the style of CL...
-bcd
--
(format nil "~:(~{~A ~}~)~(<····@·@{~A~#[~:;.~]~}~}>~)"
'(brian downing) '(bdowning lavos net))
"Seong-Kook Shin" <·····@nownuri.net> wrote in message
·································@posting.google.com...
> Greeting.
> I want to define an anonymous procedure (lambda), and that procedure
> is a recursive one.
>
> For example,
>
> (let ((foo (lambda lst)
> (if lst
> (progn (print (car lst))
> (foo (cdr lst)))))))
> (foo '(1 2)))
>
> But, of course, it didn't work.
> I just remembered that LISP namespace for variables and procedures are
> different (opposite to Scheme), if I'm correct.
Hi, I wouldn't want to go out of my depth being new to lisp and all but I
don't think the problem is the difference between variables and procdedures
namespace.
From what I understand your code should be written like the following:
> (let ((foo (lambda lst)
^ you missed a braket there it should be
(lambda (lst) ....
> (if lst
> (progn (print (car lst))
> (foo (cdr lst)))))))
^ you should use funcall i.e. (funcall foo
...)
> (foo '(1 2)))
^ similarly (funcall foo ...) as lambda function example from acl
help file (funcall (lambda (x) (+ x 3)) 4) 7
Funcall takes care of the difference between variables and functions
namespace
However I believe that the main problem of your code lies to be the fact
that within the lambda definition you are calling "foo" which has not yet
been defined - the foo within the lambda function refers to a variable
assigned outside of the let environment.
Which I think it's why in scheme you use letrec and not let (assuming that
letrec stands for recursive let) hence you need a way to find a similar tool
to letrec in common lisp, but there I cannot help you.
Raistlin
begin 666 rightarr.gif
M1TE&·····@`)`+,···@(··@8&"$A(2DI*4I*2F-C8X2$A(R,C*VMK;V]O<;&
MQM;6UN_O[____________R'Y! $```T`+ `````.``D`0 0IL,DIF5DTUX,E
B^& (*MJ4"$A))4&··@Q1,-)···@Q' SN+SJ>*C:31 ``.P``
`
end