Page 1 of 1

Lisp practice fuction not returning what I expected

Posted: Tue Jan 12, 2010 2:52 pm
by webguy08
Hi all,

I've written a function to rate games and return a list of the games with their rating, but I am having trouble getting it to return the list properly. It either returns the list with their ratings as 0 or as 1, never more like it should. I have worked on this function for a while now and no matter how many times I've rewritten it, and tried imagining what it is returning, I just can't seem to get it to work! It makes sense in my head, but doesn't seem to work in Lisp. The code is below:

Code: Select all

(setf game-list '(((title "New Super Mario Bros. Wii") (genre "Platform") (platform "Wii") (price 20))
                        ((title "Quake") (genre "First Person Shooter") (platform "PC") (price 5)))

Code: Select all

(defun rate (a-list &optional title genre platform price)
	(setf rated-games nil)
	(dolist (elem a-list)

Code: Select all

(rate game-list nil "Platform" "Wii" nil)
I'm still getting practice with lists which is why I haven't used global variables or classes. I managed to get Lisp working with my other methods the way I wanted to, but this one is just being stubborn. I would really appreciate any help.

Re: Lisp practice fuction not returning what I expected

Posted: Tue Jan 12, 2010 3:51 pm
by ramarren
First, you are using a global variable here:
webguy08 wrote:

Code: Select all

(defun rate (a-list &optional title genre platform price)
   (setf rated-games nil)
...
Remember, SETF only sets places, it doesn't create bindings. Create local lexical variables with LET.

Second, I think I explained it already that lists created with quote symbol are literals, that is, there is only one of them in memory.
webguy08 wrote:

Code: Select all

(setf elem (append elem '((rating 0))))
There is only this single ((rating 0)), and its value gets reset as you iterate over the list. If you change this line to:

Code: Select all

(setf elem (append elem (list (list 'rating 0))))
it will more or less work. I think, the code is very unidiomatic and therefore hard to read.

Re: Lisp practice fuction not returning what I expected

Posted: Tue Jan 12, 2010 4:42 pm
by webguy08
Thank you so much it works now! :D. I would probably never have guessed that was the problem. So if I use literals only one is stored in memory...hmmm. If I were to set a list to quote a (' a) then would the following diagram be roughly correct?

Code: Select all

[  | --]-->[   | --]-->[   |/]          This is a list.
  |__________|___________|
  |
[ a |/]          This is ' a.

Re: Lisp practice fuction not returning what I expected

Posted: Tue Jan 12, 2010 11:34 pm
by ramarren
webguy08 wrote:If I were to set a list to quote a (' a) then would the following diagram be roughly correct?
The string " 'a " denotes, for the reader, a literal symbol A, which is only one because of the way symbols work. The structure in your diagram would be, that is, the reader syntax for it would be " '(a) ". Note that this expands to " (quote (a)) " which is specially treated by the evaluator to produce "(a)". When in data contexts this is treated literally as a list with first element the symbol QUOTE and the rest whatever follows the quote.

And yes, for every quoted construct the implementation is allowed to create only one, and in fact for every identical quoted construct in the source the implementation is allowed to merge them into one. But, since the standard does not allow modifying them, the exact details are left to the implementation, so you cannot base anything on this behaviour, because it might change depending on the implementation and its version.

Re: Lisp practice fuction not returning what I expected

Posted: Wed Jan 13, 2010 12:45 pm
by webguy08
I think I understand that now :).