This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

changing argument variables

11 posts · 8502 views

if I have a function like....

(defun hello (x)

(setf x 6)
)

and then I ran...

(setf y 3)
(hello y)

How can I make it so that so that the value of y is changed to 6???

Re: changing argument variables

(defun new (&optional value)
  (cons value nil))

(defun value-of (ptr)
  (car ptr))

(defun (setf value-of) (new-value ptr)
  (setf (car ptr) new-value))


;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;


(defun blah (x)
  (setf (value-of x) 4321))

(defun test ()
  (let ((my-ptr (new 1234)))
    (blah my-ptr)
    (value-of my-ptr)))


;; (test) => 4321

Re: changing argument variables

LispProgrammer wrote:if I have a function like....

(defun hello (x)

(setf x 6)
)

and then I ran...

(setf y 3)
(hello y)

How can I make it so that so that the value of y is changed to 6???
In Lisp, you can't pass the variable by reference, you can only pass the value (which might itself be a reference). You can wrap the variable in a structure or in a cons cell, or you can pass the setter function.
;; Implementation of reference-to-variable
(defmacro make-reference (x)
  (let ((y (gensym)))
    `(cons (lambda () ,x) 
           (lambda (,y) (setf ,x ,y)))))

(defun dereference (ref) (funcall (car ref)))
(defun (setf dereference) (value ref) (funcall (cdr ref) value))

(defun hello (x)
  (setf (dereference x) 6))

(let ((y 3))
  (hello (make-reference y))
  (print y))
=> 6

Re: changing argument variables

LispProgrammer wrote:if I have a function like....

(defun hello (x)

(setf x 6)
)

and then I ran...

(setf y 3)
(hello y)

How can I make it so that so that the value of y is changed to 6???
Why do you want to do this? How about:
(defun hello (x) (* x 6)) ; For example

(setf x (hello x))
Even if your way were possible (which it is with a macro rather than a function), what benefit would you get? And what if the argument you pass to HELLO isn't a variable?
(hello (+ 1 2))
(hello 3)
(mapcar #'hello (list 1 2 3 4))

Re: changing argument variables

Dynamic variables would be one solution, should you really want to do this.
(defun hello (name)
   (set name 6))
=> HELLO

(defun test ()
   (let ((*a* 4))
     (declare (special *a*))
     (hello '*a*)
     *a*))
=> TEST

(test)
=> 6

Re: changing argument variables

Paul Donnelly wrote:And what if the argument you pass to HELLO isn't a variable?
The most intuitive thing to do is to treat the argument as the «place» (generalized reference, see http://www.lispworks.com/documentation/ ... /05_aa.htm). E.g., (hello (car x)) should change the car of x, (hello (slot-value some-object 'some-slot)) should change the slot of the object.
But the same syntax is impossible to use (unless hello is a macro), so argument should be wrapped into some other form (that creates the reference).

Re: changing argument variables

dmitry_vk wrote:
Paul Donnelly wrote:And what if the argument you pass to HELLO isn't a variable?
The most intuitive thing to do is to treat the argument as the «place» (generalized reference, see http://www.lispworks.com/documentation/ ... /05_aa.htm). E.g., (hello (car x)) should change the car of x, (hello (slot-value some-object 'some-slot)) should change the slot of the object.
But the same syntax is impossible to use (unless hello is a macro), so argument should be wrapped into some other form (that creates the reference).
What when the argument is the result of a more complex computation, or is constant, or is supplied by a higher-order function rather than explicitly by the programmer? My point is that while OP might like modifying the argument, it's less generally useful than the way it is normally done. Of course if you do have a good reason, places are fine for their purpose.

Re: macros

LispProgrammer wrote:
(defun hello (x)  (setf x 6))
(setf y 3)
(hello y)
How can I make it so that so that the value of y is changed to 6???
This operates on a place, so it would be a macro:
CL-USER> (defmacro setf-6 (place) `(setf ,place 6))
SETF-6
CL-USER> (let ((x 3))
           (setf-6 x)
           x)
6
--Dan B.

Re: changing argument variables

LispProgrammer wrote:How can I make it so that so that the value of y is changed to 6???
If you're doing this because you want to return multiple values (that being a common use case for pass-by-reference in C++), try (values) instead.

Re: changing argument variables

Why not do this:
(let ((y 6))
  (defun reset-y ()
    (setf y 6))
  (defun set-y (x)
    (setf y x))
  (defun get-y ()
    y))
It looks like what your doing is describing an object that has three functions. A a reset, setter, and a getter.

Re: changing argument variables

How about this:

(defparameter x 0)

(defun six (y)
(declare (ignore y))
6)

(define-modify-macro hello () six)

(hello x)

It depends what you want to use it for.