Discussion of Common Lisp
-
inexperienced
- Posts: 7
- Joined: Wed Dec 16, 2009 2:07 pm
Post
by inexperienced » Thu Jan 07, 2010 2:47 pm
On the 5th line of code below, someone has used the keyword parameter named ":all" in the body of the function definition. I am not accustomed to seeing it this way. I have used the code and it works. I just don't know why the 5th line is a valid equality test.
??
Code: Select all
(defun find-matching-chunks (chunk-spec &key (chunks :all))
(let ((found nil))
(cond ((not (chunk-spec-p chunk-spec))
(print-warning "~s is not a valid chunk-spec in call to find-matching-chunks." chunk-spec))
((eq :all chunks)
(dolist (chunk chunks)
(when (chunk-matches-spec chunk chunk-spec)
(push chunk found))))
found)
(t (print-warning "~S isa not a valid value for the :chunks keyword parameter to find-matching-chunks." chunks)))))
-
ramarren
- Posts: 613
- Joined: Sun Jun 29, 2008 4:02 am
- Location: Warsaw, Poland
-
Contact:
Post
by ramarren » Thu Jan 07, 2010 3:28 pm
As specified in
section 3.4.1.4 of CLHS the keyword parameter syntax in lambda lists can be, like it is in this case, (var init-form), which means that the second element in the form is evaluated to obtain the default value when the keyword parameter is not specified. It is the same with &optional arguments.
So, there is no keyword parameter named ":all" in the function you show, only parameter "chunks" with the default value ":all", and the fifth line checks if that parameter has the default value.
-
inexperienced
- Posts: 7
- Joined: Wed Dec 16, 2009 2:07 pm
Post
by inexperienced » Fri Jan 08, 2010 6:54 pm
Your answer was exactly what I was looking for.
Thank you a great deal!