Lisp practice fuction not returning what I expected

Discussion of Common Lisp

Lisp practice fuction not returning what I expected

Postby webguy08 » Tue Jan 12, 2010 2:52 pm

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.
Last edited by webguy08 on Thu Jan 14, 2010 6:07 am, edited 1 time in total.
webguy08
 
Posts: 14
Joined: Sat Jan 02, 2010 4:11 pm

Re: Lisp practice fuction not returning what I expected

Postby Ramarren » Tue Jan 12, 2010 3:51 pm

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.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Lisp practice fuction not returning what I expected

Postby webguy08 » Tue Jan 12, 2010 4:42 pm

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.
webguy08
 
Posts: 14
Joined: Sat Jan 02, 2010 4:11 pm

Re: Lisp practice fuction not returning what I expected

Postby Ramarren » Tue Jan 12, 2010 11:34 pm

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.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Lisp practice fuction not returning what I expected

Postby webguy08 » Wed Jan 13, 2010 12:45 pm

I think I understand that now :).
webguy08
 
Posts: 14
Joined: Sat Jan 02, 2010 4:11 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 1 guest