The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three:
- Combining several simple ideas into one compound one, and thus all complex ideas are made.
- The second is bringing two ideas, whether simple or complex, together, and setting them by one another so as to take a view of them at once, without uniting them into one, by which it gets all its ideas of relations.
- The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all its general ideas are made.
– John Locke, An Essay Concerning Human Understanding (1690)
SICP
思考,這種行為主要能分成三種概念:
- 簡單概念組合而成的 複雜概念
- 兩個概念之間的 關係
- 從概念中分離出來的 抽象概念
- 貓可以產奶 人可以產奶 => 哺乳類,所有哺乳類都可以產奶
語言,即是描述「思考概念」的機制:
- primitive expressions, which represent the simplest entities the language is concerned with
- means of combination, by which compound elements are built from simpler ones, and
- means of abstraction, by which compound elements can be named and manipulated as units.
SICP
- 描述實體
- 描述組合
- 描述抽象
「關係」應該可以視為組合,但保留原始信息。也就是 A+B 與 C 的差異。
語言的強大之處在於,能用簡單的詞語去描述複雜的概念,例如:
- 多種電子零件組合而成的 伺服器
- 多個伺服器連繫組成的 網路
- 搭載在網路上的討論網站 論壇
透過論壇一詞,輕鬆描述了複雜的概念。
為了在電腦上復現語言的力量,因此創造了程式語言。而 lisp 透過(應該是)最精簡的語法完成目標。
也就是說,至少理論上, lisp 可以用最小的力氣做最多的事。
最後是 macro 的優勢,function 沒辦法控制該不該複合關係(去除原始信息)。
當然,透過 eval 是可以做到,但相對容易出問題。
(defun f-when (cond body)
(if cond body))
(defmacro m-when (cond body)
`(if ,cond ,body))
;; function 下,(print (+ 1 2)) 一定會被執行(印出)。A+B會變成C
(f-when t (print (+ 1 2))) ; => 3 印出 3
(f-when nil (print (+ 1 2))) ; => nil 印出 3
;; 如果把關係完整的丟進去,就會完整的出來
(f-when t '(print (+ 1 2))) ; => (print (+ 1 2))
(f-when nil '(print (+ 1 2))) ; => nil
;; macro 可以保留關係(原始信息)
;; if 不通過,(print (+ 1 2)) 就不執行。
(m-when nil (print (+ 1 2))) ; => nil
;; if 通過,(print (+ 1 2)) 才執行
(m-when t (print (+ 1 2))) ; => 3 印出 3
;; py 或 js 好像都能用封裝來實現
;; 但就必需考慮是否要封裝, lisp 可以直接丟值或函數
(m-when t "a") ; => "a"
(m-when nil "a") ; => nil