编程语言 :ML
参考:https://www.cnblogs.com/whensean/p/6134063.html
http://www.cnblogs.com/ftae/p/8039291.html
WEEK2:
- 变量被赋值后不可再变(immutable)
- && is andalso ;|| is orelse; ! is not
- 语句以var 或者 fun开头
- if … then … else…
- 等于: = ; 不等于: <>
- 负数: ~value
- fun name (var: type,var :type) = expression
- let binding(var / fun) in expression
- 返回值是e的类型
- 可以在任何可以使用expression的地方使用
- 可以使用之前定义的变量
- list( type: t list)
- append: e1::e2
- 空list :[]
- null name :判断是否为空list,返回true or false
- hd name 得到 首元素;tl name 得到 尾list
- tuples(type: t1*t2*t3)
- 取出元素 : #1 name
- option(type : t option)
- SOME e :t->t option
- NONE :空option
- valOf e: t option -> t
- isSome: 判断是否为空
例子:
//输出x到y之间所有的数的list
fun append (x: int, y: int) =
if y - x <0
then []
else x::append(x+1,y)
//求最大值
fun max (x: int list)=
if null x
then NONE
else
let fun max_nonempty( x: int list) =
if null (tl x)
then hd x
else
let val tl_ans = max_nonempty(tl x)(*防止重复递归*)
in
if hd x > tl_ans
then hd x
else tl_ans
end
in
SOME(max_nonempty x)(*单个参数可不加括号*}
- immutable
- 函数式语言变量都是不可改变的
WEEK3
- records
- a = {name1:type1,name2:type2…}
- 取数:#name1 a
- tuple是一种特殊的records,及{1:type1,2:type2…}
datatype
datatype mytype = Call1 | Call2 of string | Call3 of int * int
- Call 是 constructor
- 不一定要有of