在 Windows 下配置环境
- 在 http://racket-lang.org/ 下载 Racket 并安装
- 运行开始菜单 Racket->DrRacket
- 在DrRacket的菜单 Language 里选择 Choose Language 选择 R5RS
- 点击 Run, 在下面的 REPL 里面就可以了
原子(atom)
原子 -> 数字 | 符号 | 字符串
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表达式
- 所有的atom都是S表达式
- 所有的list都是S表达式
S表达式 -> 原子 | 列表
Scheme
Scheme语言是Lisp的一种方言,它没有nil的概念,只有空表()。
Scheme语言的求值策略:Scheme语言中的函数是按值调用的(call by value),在求值函数体之前,实参会先被求值。
举例如下:
(display (* 2 3))中,(* 2 3)并不会被看成是具有3个元素的列表,而是看成乘法函数调用,在display调用之前,会先求值(* 2 3) => 6,结果是显示6。
假如,我们一定要把(* 2 3)看成是列表呢?
就需要引用它。(display '(* 2 3)) or (display (quote (* 2 3)))
此时,'(* 2 3)求值为列表(* 2 3)。
结果是显示(* 2 3)。
常用函数
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
- 永远不要对一个null list求car,cdr;
- car 返回第一个S-expression. 不一定是list 也有可能是atom;
- cdr,cons的结果仍是一个list;
- (quote ()) 是null list的一种表示;
网友评论