From: Michael R. Blair
Subject: Re: mutating params passed by ref
Date:
Message-ID: <ZIGGY.95Oct15112014@rolex.ai.mit.edu>
In article <················@engr.sgi.com> ·······@engr.sgi.com (Mike McDonald) writes:
In article <···················@biere.ai.mit.edu>, ·····@biere.ai.mit.edu (Michael R. Blair) writes:
>Well done. So, if you allow yourself to assume that KB will always be a
>non-null list, you can employ the following:
>
> (defun foo (kb)
> (setf (cdr kb) kb)
> (setf (car kb) '1)
> kb)
>
> (foo l)
>
>To effect the following:
>
> -------- --------- ---------
> KB -----> | | | | | | | | |
> | 1 | --------> | 1 | ---------> | 2 | / |
> | | | | | | | | |
> L -----> -------- --------- ---------
> ^
> |
> |
> (old L ptr was here)
>
You actually get:
+--------+
| |
V |
-------- | --------- ---------
KB -----> | | | | | | | | | |
| 1 | -----+ | 1 | ---------> | 2 | / |
| | | | | | | | |
L -----> -------- --------- ---------
^
|
|
(old L ptr was here)
What you really wanted was:
(defun foo (kb)
(setf (cdr kb) (cons (car kb) (cdr kb)))
(setf (car kb) 1)
kb)
OOPS! Mea culpa! You're right. Thanks for catching that.
--
------------------------------------------------------------------------------
Michael R. Blair --. ·····@ai.mit.edu | MIT Artificial Intelligence Lab
(617) 253-0765 \\ ···@lcs.mit.edu | MIT Labor. for Computer Science
,,Lambda Calculus... /\\_ ...uber alles!'' | 545 Technology Square--Room 439
http://www-swiss.ai.mit.edu/~ziggy/ziggy.html| Cambridge, MA USA 02139-3539