Search code examples
lispcommon-lispland-of-lisp

Lisp parenthesis question


This piece of code is from book : "Land Of Lisp" First version is from book. When I read it, i thought there are parenthesis "(" not necessary just before "at-loc-p" at 2nd line and ")" just after loc at 3rd line.

(defun person-at (loc pers per-locs)
       (labels ((at-loc-p (pers)
                 (eq (cadr (assoc pers per-locs)) loc)))
         (remove-if-not #'at-loc-p pers)))

But when I test this ,

(defun person-at (loc pers per-locs)
       (labels (at-loc-p (pers)
                 (eq (cadr (assoc pers per-locs)) loc))
         (remove-if-not #'at-loc-p pers)))

It came out :

Required arguments in AT-LOC-P don't match lambda list (CCL::FUNCNAME CCL::LAMBDA-LIST &BODY CCL::LABELS-FUNCTION-BODY).
[Condition of type CCL::SIMPLE-PROGRAM-ERROR]

I don't quiete understand. Need help. thank you.


Solution

  • The LABELS in

    (defun person-at (loc pers per-locs)
      (labels ((at-loc-p (pers)
                 (eq (cadr (assoc pers per-locs)) loc)))
        (remove-if-not #'at-loc-p pers)))
    

    has the syntax labels ((function-name lambda-list [[local-declaration* | local-documentation]] local-form*)*) declaration* form*, so you will have to provide a list of local function definitions for it to work.

    Since those local function definitions are themselves parenthesized, you will have to pass labels a list of this structure: ((fun1 (...) ...) (fun2 (...) ...) ...).

    Unfortunately, the stack trace and error message aren't very helpful in spotting the error here, since the message does not tell you that the problem is with labels, and it also isn't topmost in the trace. The 4: (CCL::NX1-LABELS ... would be a hint (debugger buffer on my local machine).

    Take a look at the documentation for labels in the Hyperspec to learn more.