Page 1 of 1

what is cd in (cd *db*)?

Posted: Sun Dec 05, 2010 7:35 pm
by mohsen
In Practical Common Lisp there's a code that helps display a list in a much cleaner form

Code: Select all

(defun dump-db ()
  (dolist (cd *db*)
    (format t "~{~a:~10t~a~%~}~%" cd)))
The syntax for dolist is described as
dolist (var list-form [result-form]) declaration* {tag | statement}*
but I i can't quite figure out what cd is in (cd *db*)

could someone help me crack this one?

That would be great!!

Thanks,
-m

Re: what is cd in (cd *db*)?

Posted: Mon Dec 06, 2010 12:16 am
by ramarren

Code: Select all

(dolist (cd *db*)
dolist (var list-form [result-form])
In the syntax descriptions in CLHS top level parentheses are omitted and [] denotes optional component. This is modified Backus-Naur Form. Knowing this you should be able to figure out what "cd" corresponds to.

Re: what is cd in (cd *db*)?

Posted: Mon Dec 06, 2010 3:07 pm
by JamesF
Hint: that's not the only place you see that symbol. Look at how it's used elsewhere in the function, and it should start to make sense.
The documentation should also help.

Re: what is cd in (cd *db*)?

Posted: Mon Dec 06, 2010 9:08 pm
by Warren Wilkinson

Code: Select all

(dolist (a '(1 2 3 4))
   --body--code--)
Could be rewritten as

Code: Select all

(destructuring-bind (a . rest) '(1 2 3 4)
   (tagbody :start 
      --body--code--
      (setf a (car rest) rest (cdr rest))
      (when rest (goto :start))))
If that helps.

Re: what is cd in (cd *db*)?

Posted: Wed Dec 08, 2010 12:56 pm
by Vivitron
dolist (var list-form [result-form]) declaration* {tag | statement}*
Cd is the var and *db* is the list-form, and the optional result-form is not used. There can be any number of declaration; there are none. There can be any number of tag or statement; there are no tags and the only statement is (format t ...).

I second Rmarren's suggestion to take a look at BNF.

Roughly: "For every album in the database, print it like this."

Historical note: when PCL was published CDs were a common physical medium for distributing albums :lol: