Reputation: 187
I have a data structure which is a list of lists, doesn't really matter.
(setf var1 create_data_Structure)
Now I need to insert a value inside the data structure to test something without var1 being modified.
(defun testsomething(data_str val)
(let ((data_str_aux data_str))
(progn (insert val data_str_aux)
(testData data_str_aux))))
Ok, doesn't really matter what I do with the rest of the function, but now if I write this:
>var1
It appears my my data structure with the value inside, but I didn't want to. I tried also this:
(defun testsomething(data_str val)
(let ((data_str_aux))
(progn (setq data_str_aux data_str)
(insert val data_str_aux)
(testData data_str_aux))))
And it stills modifies my global structure. What would you do?
Upvotes: 0
Views: 47
Reputation: 60004
In your case, aux
is just an extra reference to the same object in memory, so modifying it with insert
modifies the same object.
What you need is something like
(defun test-something (data-str val)
(let ((aux (copy-my-structure data-str)))
(insert val aux)
(test-data aux)))
E.g.,
(defparameter var1 (list 1 2 3 4))
(defun copy-my-structure (str)
(copy-list str))
(defun insert (val str) ; assumes non-NIL str
(nconc str (list val)))
(defun test-data (data)
(list (every #'numberp data)
(length data)))
(test-something var1 7)
==> (T 5)
var1
==> (1 2 3 4)
Upvotes: 2