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???
(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
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???
;; 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
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???
(defun hello (x) (* x 6)) ; For example
(setf x (hello x))
(hello (+ 1 2))
(hello 3)
(mapcar #'hello (list 1 2 3 4))
(defun hello (name)
(set name 6))
=> HELLO
(defun test ()
(let ((*a* 4))
(declare (special *a*))
(hello '*a*)
*a*))
=> TEST
(test)
=> 6
Paul Donnelly wrote:And what if the argument you pass to HELLO isn't a variable?
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).
LispProgrammer wrote:
- Code: Select all
(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???
CL-USER> (defmacro setf-6 (place) `(setf ,place 6))
SETF-6
CL-USER> (let ((x 3))
(setf-6 x)
x)
6
LispProgrammer wrote:How can I make it so that so that the value of y is changed to 6???
(let ((y 6))
(defun reset-y ()
(setf y 6))
(defun set-y (x)
(setf y x))
(defun get-y ()
y))
Users browsing this forum: No registered users and 9 guests