From: Michael R. Blair
Subject: Re: mutating params passed by ref
Date: 
Message-ID: <ZIGGY.95Oct13194119@biere.ai.mit.edu>
In article <·················@birch.csl.sri.com> ···@birch.csl.sri.com (Bob Riemenschneider) writes:

   In article <··········@agate.berkeley.edu> ·······@trombone.CS.Berkeley.EDU (Kevin Murphy) asks: 

   > ... What's going on here?

   Starting again from

	    --------        ---------
	    |   |  |        |   |   |
     L ---> | 1 | --------> | 2 | / |
	    |   |  |        |   |   |
	    --------        ---------

   > (defun foo (kb)
   >    (push '1 kb))
   > 
   > (foo l)

   On entering FOO, we have

	    --------        ---------
     KB --> |   |  |        |   |   |
	    | 1 | --------> | 2 | / |
     L ---> |   |  |        |   |   |
	    --------        ---------

   The PUSH says to create a CONS cell with 1 in its CAR and a pointer to the
   list in its CDR, and to make KB point to that new CONS cell, so we have

	    --------        --------        ---------
	    |   |  |        |   |  |        |   |   |
     KB --> | 1 | --------> | 1 | --------> | 2 | / |
	    |   |  |        |   |  |        |   |   |
	    --------        --------        ---------
			      ^
			      |
			      |
			      L

   Note that you did not evaluate anything that affects which CONS cell L
   points to.  So, on exiting the function, you have

	    --------        ---------        ---------
	    |   |  |        |   |   |        |   |   |
	    | 1 | --------> | 1 | ---------> | 2 | / |
	    |   |  |        |   |   |        |   |   |
	    --------        ---------        ---------
			      ^
			      |
			      |
			      L


   and the value of L is still (1 2).

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)

This you could define as your own procedure or macro, named PUSH-PAIR!
(or whatever you prefer... I like the ! to remind myself that this is
no ordinary PUSH... I'm usually a Scheme hacker, not a Common Lisp hacker).
-- 
------------------------------------------------------------------------------
  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
From: Mike McDonald
Subject: Re: mutating params passed by ref
Date: 
Message-ID: <1995Oct13.171234@engr.sgi.com>
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)

--

  Mike McDonald
  ·······@engr.sgi.com