Function Variables
Function Variables
I've encountered i problem I've never seen before. I have function that has variables declared at the start. One of the values is a list. The function works fine the first time i call it. But when i call it a second time the list variable retains the value it ended up with from the previous function call
The function basically makes a new list with push. But the second time i call it, it simply pushes into the list that was created from the previous function call.
I mean i setf the value of the list variable at the start of the function so really it should be reset whenever the function is called no? When i enter the defun code defining the function, and call the function again, i get the proper output but if i don't enter the defun code again before the next function call, i get the above problem. Any ideas?
BIOS
The function basically makes a new list with push. But the second time i call it, it simply pushes into the list that was created from the previous function call.
I mean i setf the value of the list variable at the start of the function so really it should be reset whenever the function is called no? When i enter the defun code defining the function, and call the function again, i get the proper output but if i don't enter the defun code again before the next function call, i get the above problem. Any ideas?
BIOS
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: Function Variables
I have many possible ideas of what might be going wrong, but you need to show me the code to see if any of those ideas apply to your case. I believe it is probably a problem regarding destructively modifying constants.
Re: Function Variables
Hey thanks for the reply.
Here's the code at the moment:
Here's the code at the moment:
Code: Select all
(defun list-maker (list*)
(let* ((current-val 0)
(new-list))
(setf new-list '(0))
(loop for i in list* do
(if (stringp i)
(setf current-val (read-from-string i))
(setf current-val i))
(print (push (+ current-val (nth 0 new-list)) new-list)))
(setf new-list (nreverse new-list))
(setf new-list (remove 0 new-list :count 1))
new-list))
Re: Function Variables
Try changing (setf new-list '(0)) to (setf new-list (list 0))
Even better,
Even better,
Code: Select all
(let* ((current-val 0)
(new-list (list 0)))
Re: Function Variables
It works! What was i doing wrong?
Re: Function Variables
If you use QUOTE, or backquote, it doesn't make a copy each time. Instead it makes the thing once, and then returns the same object each time. Hence if you set it, it changes.(It shouldn't be a problem if nothing is destructive.) It can be a pretty nasty surprise. If you think you have it, you can use stuff like COPY-LIST or COPY-TREE or composing it yourself to prevent only a single copy being moved around. I hit this problem a couple of times.
Re: Function Variables
Okay that's great to know. I had no idea. It was a surprise alright! That was a section of the main function. I couldn't understand the values i was getting out of the thing so i stripped it down to that and saw the problem. Thanks for the advice. Would have taken me aaages to figure that one out
Re: Function Variables
To be more precise, it doesn't make it at all. It returns the self-same object that is inside the QUOTE form in the source (constructed by the reader, usually).Jasper wrote:If you use QUOTE, or backquote, it doesn't make a copy each time. Instead it makes the thing once, and then returns the same object each time.