《實用Common Lisp程式設計》讀書小記

空軍發表於2013-05-16

前幾天開始讀 Peter Seibel 的《實用Common Lisp程式設計》,這是一本極佳的 Lisp 語言入門教程,讀了幾頁就被深深吸引了,嚐到學習新語言的樂趣。譯者田春是一位經驗豐富的 Common Lisp 程式設計師,翻譯非常到位,深得原著精髓。本書的 Web 站點 http://www.gigamonkeys.com/book 有英文版全書內容,外文好的童鞋可以直接在網站上閱讀。

讀到第3章時,有一處小疑惑,位於 Querying the Database 小節。為方便大家查閱起見,我在下面引用的是英文原書的內容,可在 Practical: A Simple Database 網頁搜尋 extract all 快速找到以下這些文字:

For instance, if you wanted to extract all the even elements from a list of numbers, you could use REMOVE-IF-NOT as follows:

CL-USER> (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
(2 4 6 8 10)

這是本書首次出現 '(...) 這種用法,隨後的文字解釋了 #'evenp ,但沒有解釋 '(...)

In this case, the predicate is the function EVENP, which returns true if its argument is an even number. The funny notation #' is shorthand for "Get me the function with the following name." Without the #', Lisp would treat evenp as the name of a variable and look up the value of the variable, not the function.

接著繼續出現 '(...) ,仍然沒有解釋:

You can also pass REMOVE-IF-NOT an anonymous function. For instance, if EVENP didn't exist, you could write the previous expression as the following:

CL-USER> (remove-if-not #'(lambda (x) (= 0 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10))
(2 4 6 8 10)

In this case, the predicate is this anonymous function:

(lambda (x) (= 0 (mod x 2)))

which checks that its argument is equal to 0 modulus 2 (in other words, is even). If you wanted to extract only the odd numbers using an anonymous function, you'd write this:

CL-USER> (remove-if-not #'(lambda (x) (= 1 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10))
(1 3 5 7 9)

帶著疑惑繼續閱讀,在 Removing Duplication and Winning Big 小節看到以下說明(可以在同一章搜尋 also know ):

However, you also know how to stop Lisp from evaluating a form: stick a single forward quote (') in front of it. So if you write make-comparison-expr like this, it will do what you want:

(defun make-comparison-expr (field value)
  (list 'equal (list 'getf 'cd field) value))

前文出現過 'lisp-form 這種用法,但一直沒有解釋含義,直到此處才有說明,似乎不能算 you also know 。應該在前述第一次出現 '(...) 時就加以解釋,此時說 you also know 就順理成章了。這可能是作者的一個小疏漏?

它的正式定義在第4章 Syntax and Semantics(請搜尋 An even):

An even simpler special operator is QUOTE, which takes a single expression as its "argument" and simply returns it, unevaluated. For instance, the following evaluates to the list (+ 1 2), not the value 3:

(quote (+ 1 2))

There's nothing special about this list; you can manipulate it just like any list you could create with the LIST function.

QUOTE is used commonly enough that a special syntax for it is built into the reader. Instead of writing the following:

(quote (+ 1 2))

you can write this:

'(+ 1 2)

This syntax is a small extension of the s-expression syntax understood by the reader. From the point of view of the evaluator, both those expressions will look the same: a list whose first element is the symbol QUOTE and whose second element is the list (+ 1 2).

另,第3章提到 FORMAT 最酷指令 ~R 如何用英語說一個大數:

相關文章