From: Greg Menke
Subject: Whats the difference?
Date: 
Message-ID: <r9qx8qnz.fsf@erols.com>
Hi,

I've been working on a little program which includes a recursive
routine which traverses a binary tree.  At various points, it adds
nodes by rplaca and rplacd.  I was running into problems generating (I
think) a circular list resulting in a stack overflow.  The instruction
looks like this:

(rplacd tree (list '(nil nil)))

	where tree is (nil nil), and resides somewhere in the binary
	tree.  The result is (nil (nil nil)) however after several
	iterations, I get the stack overflow.  The printed
	representation of the list appears to be infinitely nested
	nils: (nil (nil (nil ... till the stack fault.

When I did this:

(rplacd tree (list (cons 'nil (cons 'nil ()))))

	it appears to work right.

The lisp is Xlisp-stat.  Is there a difference between the two, or am
I deceiving myself?

Thanks.

Gregm

From: Pierpaolo Bernardi
Subject: Re: Whats the difference?
Date: 
Message-ID: <921072805.705016@fire-int>
Greg Menke (······@erols.com) wrote:

: The lisp is Xlisp-stat.  Is there a difference between the two, or am
: I deceiving myself?

I'd say that the lisp dialect used is very important in this case.
I don't know xlisp-stat. I'll comment as if it was CL.

: I've been working on a little program which includes a recursive
: routine which traverses a binary tree.  At various points, it adds
: nodes by rplaca and rplacd.  I was running into problems generating (I
: think) a circular list resulting in a stack overflow.  The instruction
: looks like this:

: (rplacd tree (list '(nil nil)))

'(nil nil) is a constant, and you should not modify it later.  The
compiler if free to coalesce all the occurrences of '(nil nil) to the
same structure in memory, or to put it in read-only memory.

Hope this helps.

P.
From: Stig Hemmer
Subject: Re: Whats the difference?
Date: 
Message-ID: <ekv90d5iee8.fsf@gnoll.pvv.ntnu.no>
Greg Menke <······@erols.com> writes:
> (rplacd tree (list '(nil nil)))
> (rplacd tree (list (cons 'nil (cons 'nil ()))))
> 
> The lisp is Xlisp-stat.  Is there a difference between the two, or am
> I deceiving myself?

There is definitely a difference.

The first way references a constant '(NIL NIL) that is stored in the
program.  It called multiple times, each call gets a reference to the
_same_ constant.

If you later change this with a new call to RPLACD, you change the
constant in the program.  Oops.

The last way builds a new list each time it is run.  Changing one of
these lists does _not_ change anything but that one list.  Hurray.

Stig Hemmer,
Jack of a Few Trades.

PS: I don't know Xlisp-stat, but this is common to most Lisps.

PPS:  What is the current fashion on quoting NIL?