The Little Schemer-CHP1

在 Windows 下配置环境
  1. http://racket-lang.org/ 下载 Racket 并安装
  2. 运行开始菜单 Racket->DrRacket
  3. 在DrRacket的菜单 Language 里选择 Choose Language 选择 R5RS
  4. 点击 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的一种表示;

参考文献

S表达式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,131评论 0 23
  • 点击事件传递规则 public boolean dispatchTouchEvent(MotionEvent ev...
    简单爱呵呵阅读 254评论 0 0
  • 我是谁?还没确定! 昨晚10:00后,丫丫 思涵创业平台6,快要挤破门了,家乡的人们习惯性的17/18点吃晚饭,遛...
    财富健康1阅读 365评论 0 2
  • 【读经】 申命记1章 【金句】 以下所记的是摩西在约旦河东的旷野、疏弗对面的亚拉巴,就是巴兰、陀弗、拉班、哈洗录、...
    chanor阅读 812评论 0 0