From: tachyon
Subject: lisp dilemma
Date: 
Message-ID: <1128753861.702453.128830@g44g2000cwa.googlegroups.com>
Hi, I'm new to lisp, having much fun learning. Anyways, I have a
function in which I first create a list of sublists each starting with
default values as follows:
(setf start-state '((0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0
0)(0 0 0 0 0 0)(0 0 0 0 0 0)))
Then I fill the list with (nth (nth ))'s and return it. The problem is
that when I call this function again, the function seems to completely
ignore the initial declaration of the list to the initial values (the
above setf). That is, the list is supposed to be reset from scratch,
but for some reason it keeps the previous contents (from previous
return) intact. In fact, if I put print statements before and after the
setf above, and I call the function for a second time, I get a filled
list before the setf and a filled list again after the setf.
I suspect that I am somehow creating pointers to start-state and not
really altering the elements in a deep way.
Does anyone have any idea what is going on?

Thanks.

From: JP Massar
Subject: Re: lisp dilemma
Date: 
Message-ID: <uarek1525qmabq4kuar9miq1hk779tunjm@4ax.com>
On 7 Oct 2005 23:44:21 -0700, "tachyon" <·······@gmail.com> wrote:

>Hi, I'm new to lisp, having much fun learning. Anyways, I have a
>function in which I first create a list of sublists each starting with
>default values as follows:
>(setf start-state '((0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0
>0)(0 0 0 0 0 0)(0 0 0 0 0 0)))
>Then I fill the list with (nth (nth ))'s and return it. The problem is
>that when I call this function again, the function seems to completely
>ignore the initial declaration of the list to the initial values (the
>above setf). That is, the list is supposed to be reset from scratch,
>but for some reason it keeps the previous contents (from previous
>return) intact. In fact, if I put print statements before and after the
>setf above, and I call the function for a second time, I get a filled
>list before the setf and a filled list again after the setf.
>I suspect that I am somehow creating pointers to start-state and not
>really altering the elements in a deep way.
>Does anyone have any idea what is going on?

You're not creating new list structure each time you call your
function.  You're recycling the (created once) list structure.

Do this instead:

(setf start-state (loop for j from 1 to 6 collect (make-list 6
:initial-element 0)))

Now all the levels of the list structure will be recreated each
time that statement is executed.

It's a good idea to get out of the habit of using ' to create any
list structure you might want to change.


 
From: Surendra Singhi
Subject: Re: lisp dilemma
Date: 
Message-ID: <3bnbfbq9.fsf@netscape.net>
"tachyon" <·······@gmail.com> writes:

> Thank you all for that, I was racking my brain for nothing in a 200
> line function since I didn't know the limitations of declaring a list
> with quotes. I'll be sure to check out that FAQ right away.

If your function is 200 lines long, then you definitely would want to break it
up into several smaller functions. As a rule of thumb a function should never
be longer than one screenful.

-- 
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

,----
| By all means marry; if you get a good wife, you'll be happy. If you
| get a bad one, you'll become a philosopher.  
|    -- Socrates
`----
From: Pascal Bourguignon
Subject: Re: lisp dilemma
Date: 
Message-ID: <877jcnxhxm.fsf@thalassa.informatimago.com>
Surendra Singhi <·········@netscape.net> writes:

> "tachyon" <·······@gmail.com> writes:
>
>> Thank you all for that, I was racking my brain for nothing in a 200
>> line function since I didn't know the limitations of declaring a list
>> with quotes. I'll be sure to check out that FAQ right away.
>
> If your function is 200 lines long, then you definitely would want to break it
> up into several smaller functions. As a rule of thumb a function should never
> be longer than one screenful.

A 2500x1600 pixels screen can hold easily a 200-line long function.

(/ 1600 8) = 200

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
__Pascal Bourguignon__                     http://www.informatimago.com/
From: Kenny Tilton
Subject: Re: lisp dilemma
Date: 
Message-ID: <ArP1f.9235$wf6.1821600@twister.nyc.rr.com>
tachyon wrote:
> Hi, I'm new to lisp, having much fun learning. Anyways, I have a
> function in which I first create a list of sublists each starting with
> default values as follows:
> (setf start-state '((0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0
> 0)(0 0 0 0 0 0)(0 0 0 0 0 0)))

"Gotcha!", cry Lisp tutorial authors everywhere. "We fooled you into 
thinking quoting is a good way to allocate lists."

This is I believe Lisp newby FAQ #1. It is OK to do '(a b c) if you plan 
never to mutate the list. Otherwise, it is like destructively modifying 
a C string allocated as a literal in source:

void get_whew_or_phew (int make_phew) (
      char *msg = "whew! I won't change!! I am a literal constant!";
      if (make_phew) *msg = 'p';
      return msg;
}

But you do not get a new "whew!..." each time get_whew_or_phew is 
called. Once make_phew is true (non-zero), this function always returns 
"phew....".

Likewise, the Lisp compiler allocates '(a b c) once and puts it 
somewhere, to be re-used each time the above SETF gets executed. Then 
your code changes the contents of that effectively global list. The next 
time the SETF runs, it just uses the same pointer to the now-modified list.

Phew!

:)


-- 
Kenny

Why Lisp? http://wiki.alu.org/RtL_Highlight_Film

"I've wrestled with reality for 35 years, Doctor, and I'm happy to state 
I finally won out over it."
     Elwood P. Dowd, "Harvey", 1950
From: Tayssir John Gabbour
Subject: Re: lisp dilemma
Date: 
Message-ID: <1128778759.713860.145060@g49g2000cwa.googlegroups.com>
Kenny Tilton wrote:
> tachyon wrote:
> > Hi, I'm new to lisp, having much fun learning. Anyways, I have a
> > function in which I first create a list of sublists each starting with
> > default values as follows:
> > (setf start-state '((0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0 0)(0 0 0 0 0
> > 0)(0 0 0 0 0 0)(0 0 0 0 0 0)))
>
> "Gotcha!", cry Lisp tutorial authors everywhere. "We fooled you into
> thinking quoting is a good way to allocate lists."
>
> This is I believe Lisp newby FAQ #1.

http://wiki.alu.org/Lisp_Gotchas

It is so undefined, Lisp reached out of the monitor last time I did it
and slapped me.


Tayssir

--
"Moreover, under existing conditions, private capitalists inevitably
control, directly or indirectly, the main sources of information
(press, radio, education). It is thus extremely difficult, and indeed
in most cases quite impossible, for the individual citizen to come to
objective conclusions and to make intelligent use of his political
rights."
  -- Albert Einstein
  http://www.monthlyreview.org/598einst.htm
From: GP lisper
Subject: Re: lisp dilemma
Date: 
Message-ID: <1128816184.a14107477afd1b79bcb3cfbd76de89e0@teranews>
On Sat, 08 Oct 2005 13:02:24 GMT, <·······@nyc.rr.com> wrote:
>
> This is I believe Lisp newby FAQ #1. It is OK to do '(a b c) if you plan 
> never to mutate the list.

OK, I had this code:

(defun shutouts ()
  (let ((team-shutouts (shutouts-hash))
	(results '()))
    (maphash #'(lambda (k v) (push (list k v) results)) team-shutouts)
    (sort results #'< :key #'cadr)))

CLSQL-USER> (shutouts)
(("NYY" 2) ("TB" 5) ("SEA" 5) ("BOS" 5) ("BAL" 5) ("STL" 6) ("LAA" 6) ("ATL" 6)
 ("TEX" 7) ("SF" 7) ("CWS" 7) ("FLA" 8) ("CIN" 8) ("CHC" 8) ("PIT" 9) ("PHI" 9)
 ("LAD" 9) ("ARI" 9) ("MIN" 10) ("KC" 10) ("DET" 10) ("COL" 10) ("WSH" 11)
 ("NYM" 11) ("CLE" 11) ("SD" 12) ("OAK" 12) ("MIL" 12) ("TOR" 14) ("HOU" 17))

which works, perhaps because of CMUCL.  I can change it to 
...
	(results))
...

and it still works.  My sole intention was to say 'hey lisp, this is
going to be a list'.  How would I do that:

a)	(results (list )))     [doesn't seem to work, results is a constant]
b)      ignore it, lisp doesn't need the list hint, as currently coded
c)      <a c.l.l. suggested technique>

-- 
From: GP lisper
Subject: Re: lisp dilemma
Date: 
Message-ID: <1128819787.6d4e8d631584eb8074c509fd6b1b3dfc@teranews>
On Sat, 8 Oct 2005 16:37:59 -0700, <········@CloudDancer.com> wrote:
>
> and it still works.  My sole intention was to say 'hey lisp, this is
> going to be a list'.  How would I do that:

 	(results '()))

In this case that was fine, it is the expectation that the '(x y z)
list is unchanged that is the newbie tripwire.  I expected to change
some temp var in a let.  If that form assists the compiler is still an
open question to me.

-- 
In Common Lisp quite a few aspects of pathname semantics are left to
the implementation.
From: Julian Squires
Subject: Re: lisp dilemma
Date: 
Message-ID: <ydudna_UDP1zuNTenZ2dnUVZ_tGdnZ2d@rogers.com>
On 2005-10-08, GP lisper <········@CloudDancer.com> wrote:
> and it still works.  My sole intention was to say 'hey lisp, this is
> going to be a list'.  How would I do that:

If that's your sole intention, don't you want (declare (type list
result)) instead?  (and keep result nil initially)
CMUCL probably doesn't need that hint, though.

Cheers.

-- 
Julian Squires