在 Windows 下配置環(huán)境
- 在 http://racket-lang.org/ 下載 Racket 并安裝
- 運行開始菜單 Racket->DrRacket
- 在DrRacket的菜單 Language 里選擇 Choose Language 選擇 R5RS
- 點擊 Run权逗, 在下面的 REPL 里面就可以了
原子(atom)
原子 -> 數(shù)字 | 符號 | 字符串
eg: atom肠仪、turkey认然、1492蜗帜、u科侈、*abc$...
列表(list)
由0到多個atom或者list組成四苇,用括號包圍起來
eg: (atom)装处、(atom turkey or)、((atom turkey) or)挣郭、(((how) are) ((you) (doing so)) far)...
S表達(dá)式
- 所有的atom都是S表達(dá)式
- 所有的list都是S表達(dá)式
S表達(dá)式 -> 原子 | 列表
Scheme
Scheme語言是Lisp的一種方言垃杖,它沒有nil的概念,只有空表()丈屹。
Scheme語言的求值策略:Scheme語言中的函數(shù)是按值調(diào)用的(call by value),在求值函數(shù)體之前伶棒,實參會先被求值旺垒。
舉例如下:
(display (* 2 3))中,(* 2 3)并不會被看成是具有3個元素的列表肤无,而是看成乘法函數(shù)調(diào)用先蒋,在display調(diào)用之前,會先求值(* 2 3) => 6宛渐,結(jié)果是顯示6竞漾。
假如,我們一定要把(* 2 3)看成是列表呢窥翩?
就需要引用它业岁。(display '(* 2 3)) or (display (quote (* 2 3)))
此時,'(* 2 3)求值為列表(* 2 3)寇蚊。
結(jié)果是顯示(* 2 3)笔时。
常用函數(shù)
The Five Rules
The Law of Car: the primitive car is defined only for non-empty lists
The Law of Cdr: the primitive cdr is defined only for non-empty lists.The cdr of any non-empty list is always another list.
The Law of Cons: The primitive cons takes two arguments. The first one is any S-expression; the second one is any list. The result is a list.
The Law of Null?: the primitive null? is defined only for lists.
The Law of Eq?: the primitive eq? takes two arguments. Both of them must be non-numeric atoms
- 永遠(yuǎn)不要對一個null list求car,cdr;
- car 返回第一個S-expression. 不一定是list 也有可能是atom;
- cdr,cons的結(jié)果仍是一個list;
- (quote ()) 是null list的一種表示;