Beginners question on eg in Practical Common Lisp
Posted: Thu Dec 31, 2009 5:17 am
Hello all,
I am new here, and also new to lisp. After a brief online tutorial I am looking at Practical Common Lisp.
However I have quite quickly been presented with something that I cannot quite understand.
I am referring to the query function in chapter 3 (a simple database.)
I do not understand how the select (remove-if-not... ) function and the where function (below) work in this context.
My problem is with the returns of true (t). I think that they are necessary because of the 'and' macro, which would return nil if one of its components was false. Meanwhile, the &key will automatically set the value to nil (or a set default value) if it does not receive the relevant parameter.
So what happens to the else-ish t returns in expressions such as (if title (equal (getf cd :title) title) t)? He seems to say that the clauses return t if no matching argument is received, which makes sense. Does the &key parameter override these returns? Do they fall beneath it? Where do they go?
I have not seen thus far in lisp a return within a function that the function does not itself return. Is this what happens here?
In Java, (the only other language I even slightly know) the destination of a return is always evident, as far as I know. In lisp it seems a little more complicated.
Any comments or references to online sources would be very welcome.
Thanks!
Here's the relevant function:
I am new here, and also new to lisp. After a brief online tutorial I am looking at Practical Common Lisp.
However I have quite quickly been presented with something that I cannot quite understand.
I am referring to the query function in chapter 3 (a simple database.)
I do not understand how the select (remove-if-not... ) function and the where function (below) work in this context.
My problem is with the returns of true (t). I think that they are necessary because of the 'and' macro, which would return nil if one of its components was false. Meanwhile, the &key will automatically set the value to nil (or a set default value) if it does not receive the relevant parameter.
So what happens to the else-ish t returns in expressions such as (if title (equal (getf cd :title) title) t)? He seems to say that the clauses return t if no matching argument is received, which makes sense. Does the &key parameter override these returns? Do they fall beneath it? Where do they go?
I have not seen thus far in lisp a return within a function that the function does not itself return. Is this what happens here?
In Java, (the only other language I even slightly know) the destination of a return is always evident, as far as I know. In lisp it seems a little more complicated.
Any comments or references to online sources would be very welcome.
Thanks!
Here's the relevant function:
The function looks like this:
(defun where (&key title artist rating (ripped nil ripped-p))
#'(lambda (cd)
(and
(if title (equal (getf cd :title) title) t)
(if artist (equal (getf cd :artist) artist) t)
(if rating (equal (getf cd :rating) rating) t)
(if ripped-p (equal (getf cd :ripped) ripped) t))))
This function returns an anonymous function that returns the logical AND of one clause per field in our CD records. Each clause checks if the appropriate argument was passed in and then either compares it to the value in the corresponding field in the CD record or returns t, Lisp's version of truth, if the parameter wasn't passed in. Thus, the selector function will return t only for CDs that match all the arguments passed to where.7