2,619
次編輯
Tankianting(討論 | 貢獻) |
Tankianting(討論 | 貢獻) (→1.1介紹) |
||
行 75: | 行 75: | ||
另外還有 record、array、GADT、垃圾回收等等。 | 另外還有 record、array、GADT、垃圾回收等等。 | ||
===1.2基本操作=== | |||
<pre> | |||
let () = print_string "foo" (*回傳 unit*) | |||
</pre> | |||
==== 迴圈==== | ==== 迴圈==== | ||
<pre> | <pre> | ||
行 83: | 行 87: | ||
done | done | ||
</pre> | </pre> | ||
====部分應用(不需括號)==== | ====部分應用(不需括號)==== | ||
行 95: | 行 101: | ||
</pre> | </pre> | ||
====互遞迴==== | |||
<pre> | <pre> | ||
let rec f x = ... | let rec f x = ... | ||
行 124: | 行 130: | ||
(int * int) -> int,其中的 (int * int)是 product type | (int * int) -> int,其中的 (int * int)是 product type | ||
====list==== | |||
*[]:nil | |||
*x::ls:(cons x ls) | |||
*x1@x2:concat x1 and x2 | |||
*List.length:字串長度 | |||
*List.map:map | |||
*List.iter:execute a funct90n for list | |||
*List.mem:成員是否在列表內 | |||
==== String ==== | |||
"foo" | |||
====Unit==== | |||
unit : () | |||
<pre> | |||
let f = print_string "foo" (* let f = printString("foo"); *) | |||
let f () = print_string "foo" (* let f = function () { printString("foo") ;}*) | |||
</pre> | |||
===1.3遞迴型別=== | |||
tree型別定義如下: | |||
<pre> | |||
type tree = | |||
| Node of int * tree * tree | |||
| Leaf of int | |||
</pre> | |||
以下是合規的tree | |||
<pre> | |||
let t = Node(3, Node(4, Leaf 1, Leaf 3)) | |||
</pre> | |||
====模式比對==== | |||
<pre> | |||
let sum foo x = | |||
match x with | |||
| Node (n , t1, t2) -> n + sum t1 + sum t2 | |||
| Leaf n -> n | |||
</pre> | |||
guard | |||
<pre>|Leaf n when n > 0 -> ...</pre> | |||
語法糖 | |||
<pre>let f = function ... 即 let f x = match x with ...</pre> | |||
[[分類:資訊]] | [[分類:資訊]] | ||
[[分類:邏輯學]] | [[分類:邏輯學]] |