I expected this function to cons all the intermediate results into a
list; instead, it returns an empty list. What's wrong?
thanx
(defun tester (x)
(let ((orbit '()))
(labels ((p (n)
(cond
((<= n 1) orbit)
(t (progn
(cons n orbit)
(p (1- n)))))))
(p x))))
Sent via Deja.com
http://www.deja.com/
·······@my-deja.com writes:
> I expected this function to cons all the intermediate results into a
> list; instead, it returns an empty list. What's wrong?
>
> thanx
>
> (defun tester (x)
> (let ((orbit '()))
> (labels ((p (n)
> (cond
> ((<= n 1) orbit)
> (t (progn
> (cons n orbit)
^^^^^^^^^^^^^^
> (p (1- n)))))))
> (p x))))
>
You cons a list but you don't change the value of orbit.
Change the underlined form to (setf orbit (cons n orbit)) and you'll
see a difference.
--
Lieven Marchand <···@wyrd.be>
Gla�r ok reifr skyli gumna hverr, unz sinn b��r bana.
Also, don't do (let ((orbit '())) ..., instead do (let ((orbit nil)), as you
can run into trouble modifying a quoted list.
Lieven Marchand <···@wyrd.be> wrote in message
···················@localhost.localdomain...
> ·······@my-deja.com writes:
>
> > I expected this function to cons all the intermediate results into a
> > list; instead, it returns an empty list. What's wrong?
> >
> > thanx
> >
> > (defun tester (x)
> > (let ((orbit '()))
> > (labels ((p (n)
> > (cond
> > ((<= n 1) orbit)
> > (t (progn
> > (cons n orbit)
> ^^^^^^^^^^^^^^
> > (p (1- n)))))))
> > (p x))))
> >
>
> You cons a list but you don't change the value of orbit.
>
> Change the underlined form to (setf orbit (cons n orbit)) and you'll
> see a difference.
>
> --
> Lieven Marchand <···@wyrd.be>
> Gla�r ok reifr skyli gumna hverr, unz sinn b��r bana.
In article <············@sloth.swcp.com>,
Jim Bushnell <········@swcp.com> wrote:
>Also, don't do (let ((orbit '())) ..., instead do (let ((orbit nil)), as you
>can run into trouble modifying a quoted list.
You can't modify the empty list. nil and '() are functionally identical,
as if:
(defconstant nil '())
had been done.
--
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
* Jim Bushnell wrote:
> Also, don't do (let ((orbit '())) ..., instead do (let ((orbit nil)), as you
> can run into trouble modifying a quoted list.
But not that one!
Seriously, I do this all the time and I think it's good style: when I
mean the empty list I say '(), when I mean not true I say NIL. I
expect others differ -- it's obviously a matter of taste. For what
it's worth I also sometimes say ':foo when I want to talk about a
symbol which just happens to be in the keyword package, and :foo when
I am calling a fn which takes a KW arg...
--tim
In article <················@naggum.net>, Erik Naggum wrote:
>* "Jim Bushnell" <········@swcp.com>
>> Also, don't do (let ((orbit '())) ..., instead do (let ((orbit nil)), as
>> you can run into trouble modifying a quoted list.
>
> Nobody is modifying a quoted list in this case. Using nil communicates
> something else entirely from the empty list, which doesn't need to be
are you sure? i gave not quoting ( ) a while ago when one of the
implementations i was using returned an error message (don't remember which
one)
> ...
hs
In article <··············@localhost.localdomain>,
Lieven Marchand <···@wyrd.be> wrote:
>·······@my-deja.com writes:
>
>> I expected this function to cons all the intermediate results into a
>> list; instead, it returns an empty list. What's wrong?
>>
>> thanx
>>
>> (defun tester (x)
>> (let ((orbit '()))
>> (labels ((p (n)
>> (cond
>> ((<= n 1) orbit)
>> (t (progn
>> (cons n orbit)
> ^^^^^^^^^^^^^^
>> (p (1- n)))))))
>> (p x))))
>>
>
>You cons a list but you don't change the value of orbit.
>
>Change the underlined form to (setf orbit (cons n orbit)) and you'll
>see a difference.
And since this is such a common thing to do, Common Lisp provides a macro
for it: (push n orbit)
--
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.