Hacker News new | past | comments | ask | show | jobs | submit login

Is it just me, or is there a typo in the following?

  CL-USER> (remove-if-not
    #'(lambda (cd) (equal (getf cd :artist) "Dixie Chicks")) *db*)
  ((:TITLE "Home" :ARTIST "Dixie Chicks" :RATING 9 :RIPPED T)
   (:TITLE "Fly" :ARTIST "Dixie Chicks" :RATING 8 :RIPPED T))
It seems to be missing a single quote... shouldn't it be:

  CL-USER> (remove-if-not
    #'(lambda (cd) (equal (getf cd :artist) "Dixie Chicks"))' *db*)
  ((:TITLE "Home" :ARTIST "Dixie Chicks" :RATING 9 :RIPPED T)
   (:TITLE "Fly" :ARTIST "Dixie Chicks" :RATING 8 :RIPPED T))
If this is a typo, it merely shows how incredibly well written this is, because I have literally never read lisp code in any seriousness in my life!



In your example, the LAMBDA form is preceded by a sharpquote. So #'FORM is syntactic sugar for (FUNCTION FORM). This is a way to reference a value in the function namespace instead of the variable namespace (Common Lisp is a "Lisp-2"... actually more than that, but that's the common terminology.) Usually this sharpquote would precede names of functions when they are passed as a value. (Interestingly, they are optional on the LAMBDA form.)

Generally, preceding single quotes in Common Lisp e.g. 'FORM are syntactic sugar for (QUOTE FORM) which has the meaning of reading in the form without evaluating it. You'll see lots of single quotes used for that purpose as well.


Oh, I see. In that case, I was a little confused by the following:

  CL-USER> (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
  (2 4 6 8 10)
The author explained what #'evenp did (FUNCTION '(1 2 3 4 5 6 7 8 9 10)) but not that '(1 2 3 4 5 6 7 8 9 10) is just (1 2 3 4 5 6 7 8 9 10).

In other words, the above evaluates to:

  (FUNCTION evenp (1 2 3 4 5 6 7 8 9 10))
Or didn't explain it unless, of course, I missed a bit :-)


You can see how an expression is read this way:

    (defun how-is-it-read? (string)
      (write-to-string (read-from-string string)
                       :pretty nil))
    ==> HOW-IS-IT-READ?

    (how-is-it-read? "#'foo")
    ==> "(FUNCTION FOO)"

    (how-is-it-read? "(remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))")
    ==> "(REMOVE-IF-NOT (FUNCTION EVENP) (QUOTE (1 2 3 4 5 6 7 8 9 10)))"
You can see that this does not match your expectation. Are you actually trying to type that code and evaluate it at a Lisp listener? If not, you should.

Now, FUNCTION is a special operator that in this case returns the function named EVENP.

QUOTE is a special operator that just returns the object it is passed.

So REMOVE-IF-NOT will get called with two arguments: the function named EVENP, and a list of integers from 1 to 10.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: