From: ·······@my-deja.com
Subject: let and label usage
Date: 
Message-ID: <95n2p9$feq$1@nnrp1.deja.com>
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/

From: Lieven Marchand
Subject: Re: let and label usage
Date: 
Message-ID: <m3wvb4x4xo.fsf@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.
From: Jim Bushnell
Subject: Re: let and label usage
Date: 
Message-ID: <95n80f$2ar$1@sloth.swcp.com>
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.
From: Barry Margolin
Subject: Re: let and label usage
Date: 
Message-ID: <7OFf6.18$oX2.3939@burlma1-snr2>
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.
From: Tim Bradshaw
Subject: Re: let and label usage
Date: 
Message-ID: <ey366io4rdp.fsf@cley.com>
* 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
From: Hartmann Schaffer
Subject: Re: let and label usage
Date: 
Message-ID: <slrn97uvh3.tmh.hs@paradise.nirvananet>
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
From: Barry Margolin
Subject: Re: let and label usage
Date: 
Message-ID: <UqFf6.16$oX2.4006@burlma1-snr2>
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.