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