Programming Language A 学习笔记(二)
1. 语法糖——元组的“名称引用”与“位置引用”:
(e1,...,en) <=> {1=e1,...,n=en}
类型:t1 * … * tn <=> {1:t1,...,n:tn}
2. 自定义数据类型绑定:
datatype mytype = TwoInts of int * int
| Str of string
| Pizza
3. 访问自定义数据类型的值:
fun f x = (* f has type mytype -> int *)
case x of
Pizza => 3
| TwoInts(i1, i2) => i1 + i2
| Strs=>String.sizes
4. 数据类型绑定与Case表达式简化描述:
datatype t = C1 of t1 | C2 of t2 | … | Cn of tn
case e of p1 => e1 | p2 => e2 | … | pn => en
5. 类型别名(Type Synonyms)
type foo = int
6. 自定义列表类型
datatype my_int_list = Empty
| Cons of int * my_int_list
7. 多态类型(Polymorphic Datatypes)
datatype 'a option = NONE | SOME of 'a
二叉树定义:
datatype ('a, 'b) tree = Node of 'a * ('a, 'b) tree * ('a, 'b) tree
| Leaf of 'b
8. Val-Binding的真相——模式匹配(Pattern-Matching for Each-Of Types)
例8-1-1:
fun sum_triple (triple : int * int * int) =
case triple of
(x, y, z) => z + y + x
例8-2-1:
fun full_name (r : {first : string, middle : string, last : string}) =
case r of
{first = x, middle = y, last = z} => x ^ "" ^ y ^ "" ^ z
例8-2-2:
fun full_name (r : {first : string, middle : string, last : string}) =
let val {first = x, middle = y, last = z} = r
in
x ^ "" ^ y ^ "" ^ z
end
例8-1-2:
fun sum_triple (triple : int * int * int) =
let val (x, y, z) = triple
in
x + y + z
end
例8-2-3:
fun full_name {first = x, middle = y, last = z} =
x ^ "" ^ y ^ "" ^ z
例8-1-3:
fun sum_triple (x, y, z) =
x + y + z
9. 题外话——类型推导(Type inference)
In ML,every variable and function has a type (or your program fails to type-check)—
type inference only means you do not need to write down the type.
10. 题外话——多态类型与等价类型
'a list * 'a list -> 'a list
可以替换为:string list * string list -> string list
不能替换为:string list *int list -> string list
'a 必须替换为同样的数据类型
11. 嵌套模式(Nested Patterns)
a::(b::(c::d)) 包含至少3个元素的list
a::(b::(c::[])) 只包含3个元素的list
模式匹配的递归定义(the elegant recursive denition of pattern matching)
|
1 |
A variable pattern(x) matches any value v and introduces one binding (from x to v). |
|
2 |
The pattern C matches the value C,if C is a constructor that carries no data. |
|
3 |
The pattern C p where C is a constructor and p is a pattern matches a value of the form C v (notice the constructors are the same) if p matches v (i.e., the nested pattern matches the carried value). It introduces the bindings that p matching v introduces. |
|
4 |
The pattern (p1,p2,...,pn) matches a tuple value (v1,v2,...,vn) if p1 matches v1 and p2 matches v2, ..., and pn matches vn. It introduces all the bindings that the recursive matches introduce. |
|
5 |
(A similar case for record patterns of the form {f1=p1, … , fn=pn} ...) |
例11-1-1:
fun len xs =
case xs of
[] => 0
| x::xs' => 1 + len xs'
例11-1-2:
fun len xs =
case xs of
[] => 0
| _::xs' => 1 + len xs'
通配符(wildcard) (_) 指代任意没有定义数据类型的值
12. 可用的嵌套模式范例:
例12-1:
exception BadTriple
fun zip3 list_triple =
case list_triple of
([], [], []) => []
| (hd1::tl1, hd2::tl2, hd3::tl3) => (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| _ => raiseBadTriple
fun unzip3 lst =
case lst of
[] => ([], [], [])
| (a, b, c)::tl => let val (l1, l2, l3) = unzip3 tl
in
(a::l1, b::l2, c::l3)
end
例12-2:
datatype sgn = P | N | Z
fun multsign (x1, x2) =
let fun sign x = if x = 0 then Z else if x > 0 then P else N
in
case(sign x1,sign x2) of
(Z, _) => Z
| (_, Z) => Z
| (P, P) => P
| (N, N) => P
| _ => N (* many say bad style; I am okay with it *)
end
13. 多重选择的函数定义(Multiple Cases in a Function Binding)
例13-1:
datatype exp = Constant of int | Negate of exp | Add of exp * exp | Multiply of exp * exp
fun eval(Constant i) = i
| eval(Negate e2) = ~(eval e2)
| eval(Add(e1, e2)) = (eval e1) + (eval e2)
| eval(Multiply(e1, e2))=(eval e1) * (eval e2)
fun append ([], ys) = ys
| append (x::xs', ys) = x::append(xs', ys)
一般形态(语法糖形式):
fun f p1 = e1
| f p2 = e2
...
| f pn = en
普通写法:
fun f x =
case x of
p1 => e1
| p2 => e2
...
| pn => en
14. 异常(Exception)
输出异常(关键字) raise:raise List.Empty
定义异常(关键字)exception:exception MyUndesirableCondition
15. 尾递归和累加器
fun sum1 xs =
case xs of
[] => 0
| i::xs' => I + sum1 xs'
16. 尾递归的定义
递归的调用出现在尾位置(saying a call is a tail call if it is in tail position.)
尾位置定义:
|
1 |
In fun f(x) = e, e is in tail position. |
|
2 |
If an expression is not in tail position, then none of its sub expressions are in tail position. |
|
3 |
If if e1 then e2 else e3 is in tail position, then e2 and e3 are in tail position (but not e1).(Case-expressions are similar.) |
|
4 |
If let b1 … bn in e end is in tail position,then e is in tail position (but no expressions in the bindings are). |
|
5 |
Function-call arguments are not in tail position. |
Programming Language A 学习笔记(二)的更多相关文章
- Learning ROS for Robotics Programming Second Edition学习笔记(二) indigo tools
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- SQL Expression Language Tutorial 学习笔记二
11. Using Textual SQL 直接使用 SQL 如果实在玩不转, 还是可以通过 test() 直接写 SQL. In [51]: s = text( ...: "SELECT ...
- Programming Language A 学习笔记(一)
SML(一) 1. ML是一个函数式编程语言,理论基础为λ演算. 2. 变量声明 val x = e; 标准类型:单元(unit).布尔(bool).整型(int).字符串(string).实数(re ...
- 《The C Programming Language》学习笔记
第五章:指针和数组 单目运算符的优先级均为2,且结合方向为自右向左. *ip++; // 将指针ip的值加1,然后获取指针ip所指向的数据的值 (*ip)++; // 将指针ip所指向的数据的值加1 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- 转)delphi chrome cef3 控件学习笔记 (二)
(转)delphi chrome cef3 控件学习笔记 (二) https://blog.csdn.net/risesoft2012/article/details/51260832 原创 2016 ...
- WPF的Binding学习笔记(二)
原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
随机推荐
- [转]undo log与redo log原理分析
数据库通常借助日志来实现事务,常见的有undo log.redo log,undo/redo log都能保证事务特性,这里主要是原子性和持久性,即事务相关的操作,要么全做,要么不做,并且修改的数据能得 ...
- linux 下C++查询mysql数据库
上一节我们看了怎么使用mysql提供的API来连接mysql数据库,现在来看看怎么执行一条简单的查询语句,并且把查询的结果显示出来. 准备工作:首先新建了一个数据库inote,在这个数据库下面新建了一 ...
- 在程序中执行shell命令
在linux系统下的操作中我们会经常用到shell命令来进行,一开始学习进程的时候对于shell命令也进行了思考,认为shell命令就是一个进程的外壳,经过了后来的学习对于这一点也有了更多的认识. 用 ...
- WinForm各种API---时时更新
好文要顶 关注我 收藏该文 徐淳 关注 - 1 粉丝 - 3 0 0 本文原文地址:http://www.cnblogs.com/hqxc/p/6160685.html 徐淳 [D ...
- C#实现快速排序
网上很多关于快速排序的教程,嗯,不错,版本也很多,有的试了一下还报错..呵呵 于是乎低智商的朕花了好几天废了8张草稿纸才弄明白.. 快速排序的采用的分治啊挖坑填数啊之类的网上到处都是,具体过程自己百度 ...
- mybatis批量删除提示类型错误
一. 这里主要考虑两种参数类型:数组或者集合. 而这点区别主要体现在EmpMapper.xml文件中标签的collection属性: 当collection="array"时,表名 ...
- HTTP Header 详解
HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...
- 49. 3种方法实现复杂链表的复制[clone of complex linked list]
[本文链接] http://www.cnblogs.com/hellogiser/p/clone-of-complex-linked-list.html [题目] 有一个复杂链表,其结点除了有一个ne ...
- appium 自动化测试之知乎Android客户端
appium是一个开源框架,相对来说还不算很稳定.转载请注明出处!!!! 前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecraz ...
- java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
分析:android 4.2.X及以下的版本,addHeaderView必须在setAdapter之前,否则会抛出IllegalStateException. android 4.2.X(API 17 ...