Page 1 of 1

Unknown Use of Keyword Parameter

Posted: Thu Jan 07, 2010 2:47 pm
by inexperienced
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)))))

Re: Unknown Use of Keyword Parameter

Posted: Thu Jan 07, 2010 3:28 pm
by ramarren
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.

Re: Unknown Use of Keyword Parameter

Posted: Fri Jan 08, 2010 6:54 pm
by inexperienced
Your answer was exactly what I was looking for.
Thank you a great deal!