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.

Function Variables

8 posts · 1503 views

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

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:
(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,
(let* ((current-val 0)
       (new-list (list 0)))

Re: Function Variables

It works! :D 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 :P

Re: Function Variables

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.
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).