Function Variables

Discussion of Common Lisp
Post Reply
BIOS
Posts: 15
Joined: Sun Apr 04, 2010 1:55 pm

Function Variables

Post by BIOS » Sat Apr 17, 2010 2:40 pm

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

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Function Variables

Post by gugamilare » Sat Apr 17, 2010 2:53 pm

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.

BIOS
Posts: 15
Joined: Sun Apr 04, 2010 1:55 pm

Re: Function Variables

Post by BIOS » Sat Apr 17, 2010 3:06 pm

Hey thanks for the reply.

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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Function Variables

Post by nuntius » Sat Apr 17, 2010 3:21 pm

Try changing (setf new-list '(0)) to (setf new-list (list 0))

Even better,

Code: Select all

(let* ((current-val 0)
       (new-list (list 0)))

BIOS
Posts: 15
Joined: Sun Apr 04, 2010 1:55 pm

Re: Function Variables

Post by BIOS » Sat Apr 17, 2010 3:28 pm

It works! :D What was i doing wrong?

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Function Variables

Post by Jasper » Sat Apr 17, 2010 3:35 pm

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.

BIOS
Posts: 15
Joined: Sun Apr 04, 2010 1:55 pm

Re: Function Variables

Post by BIOS » Sat Apr 17, 2010 3:41 pm

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

Paul
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: Function Variables

Post by Paul » Wed Apr 21, 2010 11:47 pm

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

Post Reply