webguy08 wrote:Code: Select all
(setf list ((title "New Super Mario Bros. Wii")
(genre "Platform")
(platform "Wii")))
This doesn't even work, since there is an unquoted non-valid form there, but even assuming that there it was quoted this is invalid behaviour, since you
SETF is used to set places, not create variables. Most implementations will create a global variable, but this is not standard. Use
DEFPARAMETER to create global variables. Also, it is an usual convention to surround the name of global variables with 'earmuffs', that is, stars, '*list*', to make it obvious that their binding is dynamic.
In any case, one of your problems here is that you can, in fact, manipulate a local variable, and you do. And then, since it is local, in immediately goes out of scope and you lose results of your manipulation.
Like in here:
Code: Select all
(setf element (append element '((rating 0))))
you just change the local iteration variable, without touching anything else.
Also, what do you expect:
Code: Select all
(dolist (e element) ; e will be each attibute in the element, such as (genre "Platform").
(cond ((equal (second e) title) (+ game-rating 1))
((equal (second e) genre) (+ game-rating 1))
((equal (second e) platform) (+ game-rating 1))))
to do? Function '+' computes and returns a sum of two numbers, it doesn't change anything. Not to mention that it doesn't really make sense for other reasons, what if the title is the same as the platform?
In general, it is best to treat lists in Common Lisp as functional datastructures, since while they can be mutated, it doesn't always do what you expect, as there are no actual lists in Common Lisp as a complete
ADT. If you want to use mutable records, I would suggest using
classes. Or at the very least a
hash table.