Scheme初探

Scheme programs consist of expressions( everything is expressions ), which can be:
  •Primitive expressions: 2, 3.3, true, +, quotient, ...
  •Combinations: (quotient 10 2), (not true), ...

1. Primative expressions

Numbers are self-evaluating; symbols(just like name in Python) are bound to values

scm> 123
123
scm> 123.123
123.123
scm> #t
#t
scm> #f
#f
scm> "asdf"
"asdf"
scm> ’a ; this is a symbol
a

when you use the single quote, you’re telling Scheme not to follow the normal rules of evaluation and just have the symbol return as itself.

2. Function Calls
Call expressions include an operator and 0 or more operands in parentheses

scm> (+ 1 2)
3
scm> (- 2 3)
-1
scm> (* 6 3)
18
scm> (/ 5 2)
2.5
scm> (+ 1 (* 3 4))
13

some userful functions

+, -, *, /
• eq?, =, >, >=, <, <=

3. Special forms

A combination that is not a call expression is a special form:
  • if expression: (if <condition> <then> <else>)
  • and  or not: (and <e1> ... <en>), (or <e1> ... <en>), (not <en>)
  • Binding symbols: (define <symbol> <expression>)
  • New procedures: (define (<symbol> <formal parameters>) <body>)

scm> (if ’this-evaluates-to-true 1 2)
1
scm> (and 1 2 3)
3
scm> (or 1 2 3)
1

 Defining functions

scm> (define (square x) (* x x))
square
scm> (square 5)
25

Lambda Expressions
Lambda expressions evaluate to anonymous procedures
  (lambda (<formal-parameters>) <body>)
Two equivalent expressions:

(define (plus4 x) (+ x 4))
(define plus4 (lambda (x) (+ x 4)))

Ps:When you do (define ( ) ), Scheme will automatically transform it to (define (lambda () ). In this way, lambdas are more central to Scheme than they are to Python.

4. Compound values
Pairs and Lists
  • cons: Two-argument procedure that creates a pair
  • car: Procedure that returns the first element of a pair
  • cdr: Procedure that returns the second element of a pair
  • nil: The empty list

Pairs

> (define x (cons 1 2))
> x
(1 . 2)   #Dots can be used in a quoted list to specify the second element of the final pair.
> (car x)
1
> (cdr x)Dots can be used in a quoted list to specify the second element of the final pair.
2
> (cons 1 (cons 2 (cons 3 (cons 4 nil))))
(1 2 3 4)

List

scm> ’()
()
scm> nil
()
scm> (cons 1 (cons 2 nil))
(1 2)
scm> (cons 1 (cons 2 (cons 3 nil)))
(1 2 3)
scm> ’(1 2 3)
(1 2 3)

5. Symbolic Programming

Symbols normally refer to values

> (define a 1)
> (define b 2)
> (list a b)
(1 2)

Quotation is used to refer to symbols directly in Lisp.

> (list ‘a ‘b)
(a b)
> (list ‘a b)
(a 2)

Quotation can also be applied to combinations to form lists.

 > (car ‘(a b c))
 a
 > (cdr ‘(a b c))
 (b c)

Ps:scheme 中没有while, for等, 所有要用到迭代的地方都用递归

2015-07-15

时间: 2024-12-13 06:10:26

Scheme初探的相关文章

【原创】Themida 2260 虚拟机 FISH 初探(一)

标 题: [原创]Themida 2260 虚拟机 FISH 初探(一)作 者: xiaohang时 间: 2016-03-03,00:39:37链 接: http://bbs.pediy.com/showthread.php?t=208207 引用: 标 题: [原创]Themida 2260 虚拟机 FISH 初探 (二)作 者: xiaohang时 间: 2016-03-03,14:35:18链 接: http://bbs.pediy.com/showthread.php?t=208217

[译]可编程硬件Arduino初探(4)-MultiWii之config.h

0.前言 从可编程硬件初探(4)开始的一系列文章将集中进行一些优秀原文的翻译.本文为MultiWii系列的第一个翻译,Config.h 原文参考:http://www.multiwii.com/wiki/index.php?title=Config.h config.h \文件 How it works \基本原理 The first configuration of MultiWii is done in the config.h file of the firmware source. Wi

【E2LSH源码分析】p稳定分布LSH初探

上一节,我们分析了LSH算法的通用框架,主要是建立索引结构和查询近似最近邻.这一小节,我们从p稳定分布LSH(p-Stable LSH)入手,逐渐深入学习LSH的精髓,进而灵活应用到解决大规模数据的检索问题上. 对应海明距离的LSH称为位采样算法(bit sampling),该算法是比较得到的哈希值的海明距离,但是一般距离都是用欧式距离进行度量的,将欧式距离映射到海明空间再比较其的海明距离比较麻烦.于是,研究者提出了基于p-稳定分布的位置敏感哈希算法,可以直接处理欧式距离,并解决(R,c)-近邻

PHP Stream API初探

和SPL一样,在PHP手册里面,流被划为"other basic extensions"里面,是PHP开发中一个容易被忽视的函数系列.但其实在C++/Java中,流是一个很重要的概念.流的概念源于UNIX中的管道,管道是一条不间断的字节流,用来实现程序和进程间通信,或者读写外设,外部文件等. 流的概念是在php 4.3.0中被引入的.我们知道,文件操作,网络操作,数据压缩操作等具有一定的共性,比如线性的读/写或者随机定位,流就是用来把这些操作抽象出一个统一的接口以供开发者们使用,因此&

Python 3 初探,第 2 部分: 高级主题

Python 3 是 Guido van Rossum 功能强大的通用编程语言的最新版本.它虽然打破了与 2.x 版本的向后兼容性,但却清理了某些语法方面的问题.本文是这个由两部分组成的系列文章中的第二篇,本文构建在此系列 前一期文章 的基础之上,内容涵盖了 Python 更多的新特性和更高深的一些主题,比如在抽象基类.元类和修饰符等方面的变化. Cesar Otero, 顾问, 自由职业 2009 年 5 月 04 日 内容 有关 Python 版本 3—,也即 Python 3000 或 P

进阶之初探nodeJS

一.前言 在"初探nodeJS"随笔中,我们对于node有了一个大致地了解,并在最后也通过一个示例,了解了如何快速地开启一个简单的服务器. 今儿,再次看了该篇随笔,发现该随笔理论知识稍多,适合初级入门node,固萌生一个想法--想在该篇随笔中,通过一步步编写一个稍大一点的node示例,让我们在整体上更加全面地了解node. so,该篇随笔是建立在"初探nodeJS"之上的,固取名为"进阶之初探nodeJS". 好了,侃了这多,那么我们即将实现一个

从273二手车的M站点初探js模块化编程

前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数据. 273这个M站点是产品推荐我看的.第一眼看这个产品时我就再想他们这个三次加载和翻页按钮的方式,那么小分页的pageIndex是怎么计算的.所以就顺便看了下源码. 提到看源码时用到了Chrome浏览器的格式化工具(还是朋友推荐我的,不过这个格式化按钮的确不明显,不会的话自行百度). 三次加载和分

[转载]HDFS初探之旅

转载自 http://www.cnblogs.com/xia520pi/archive/2012/05/28/2520813.html , 感谢虾皮工作室这一系列精彩的文章. Hadoop集群(第8期)_HDFS初探之旅 1.HDFS简介 HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处理超大文件的需求而开发的,可以运行于廉价的商用服务器上.它所具有的高容错.高可靠性.高可扩展性.高

Xcode6.4注册URL Scheme步骤详解

URL Scheme的作用 我们都知道苹果手机中的APP都有一个沙盒,APP就是一个信息孤岛,相互是不可以进行通信的.但是iOS的APP可以注册自己的URL Scheme,URL Scheme是为方便app之间互相调用而设计的.我们可以通过系统的OpenURL来打开该app,并可以传递一些参数. 例如:你在Safari里输入www.alipay.com,就可以直接打开你的支付宝app,前提是你的手机装了支付宝.如果你没有装支付宝,应该显示的是支付宝下载界面,点击会跳到AppStore的支付宝下载