From: Steve Marx
Subject: let/setf question
Date: 
Message-ID: <pan.2002.03.25.04.37.48.670577.11654@mail.rochester.edu>
A student of mine encountered some odd behavior of "let" variables
receiving old values in a function she was writing.  I've managed to
simplify the offending code to the following, with the resulting output:

: (dotimes (i 2) (let ((x)) (setq x '(0)) (print x) (setf (nth 0 x) 1)
	(print x)))

(0) 
(1) 
(1) 
(1) 
NIL

It seems that the loop and the let are both necessary conditions for
getting the (mis)behavior.  It also seems that the quoted list '(0) is
necessary, as (list 0) seems to give the correct output.

Any ideas would be greatly appreciated; I've been unable to come up with
any reasonable explanation.  I'm using Allegro Common Lisp Trial Edition
6.1 on Linux (x86).  The student encountered the problem with the
Enterprise Edition.  Is this an Allegro Common Lisp bug? or correct LISP
behavior of some sort?

Thanks for your help.

	Steve

From: Rahul Jain
Subject: Re: let/setf question
Date: 
Message-ID: <87y9gh2da4.fsf@photino.sid.rice.edu>
Steve Marx <······@mail.rochester.edu> writes:

> : (dotimes (i 2) (let ((x)) (setq x '(0)) (print x) (setf (nth 0 x) 1)
> 	(print x)))

You're modifying a constant list. In some implementations, you'll get
an error. Others will just change the global value. It's "undefined"
what happens. Try using (setq x (list 0))

-- 
-> -/                        - Rahul Jain -                        \- <-
-> -\  http://linux.rice.edu/~rahul -=-  ············@techie.com   /- <-
-> -/ "Structure is nothing if it is all you got. Skeletons spook  \- <-
-> -\  people if [they] try to walk around on their own. I really  /- <-
-> -/  wonder why XML does not." -- Erik Naggum, comp.lang.lisp    \- <-
|--|--------|--------------|----|-------------|------|---------|-----|-|
   (c)1996-2002, All rights reserved. Disclaimer available upon request.
From: Takehiko Abe
Subject: Re: let/setf question
Date: 
Message-ID: <keke-2503022311070001@solg4.keke.org>
In article <····································@mail.rochester.edu>,
Steve Marx <······@mail.rochester.edu> wrote:

> It seems that the loop and the let are both necessary conditions for
> getting the (mis)behavior.  It also seems that the quoted list '(0) is
> necessary, as (list 0) seems to give the correct output.

'(0) is a literal object which is immutable. You must use (list 0).

Hyperspec says:
  
  The consequences are undefined if literal objects (including quoted
  objects) are destructively modified. 

  [3.8 The Evaluation and Compilation Dictionary-->QUOTE]

This restriction is similar to that of C's string constants.
See also the Common Lisp FAQ "3. Common Pitfalls".

  [3-13] Why does my program's behavior change each time I use it?

<http://www-2.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/part3/faq-doc-14.html>

-- 
<keke at mac com>
Are you sure that sound might want to have an idiot?