|
|
行 29: |
行 29: |
|
| |
|
| == Ch1 有型別的函數式程設 == | | == Ch1 有型別的函數式程設 == |
| ===1.1介紹===
| |
| <pre>(* 我是註解 *)
| |
|
| |
|
| print_endline "string" (* 函數使用,一般寫法:print_endline("string")*)
| | * [[Program=Proof筆記-第1章]] |
|
| |
|
| </pre>
| |
|
| |
| ocamlopt 可以編譯
| |
|
| |
| <pre>
| |
|
| |
| # 2 + 2 ;;
| |
| -: int = 4
| |
| (* 對結果做型別推導 *)
| |
|
| |
| let s = string_to_int 3.2
| |
| (* -> 這會型別錯誤 *)
| |
|
| |
| </pre>
| |
|
| |
| ====Type annotation(型別顯式標記)====
| |
|
| |
| 比較:
| |
| <pre>
| |
| let f x = x + 1
| |
| let f (x : int ) : int = x + 1
| |
|
| |
| </pre>
| |
|
| |
| ====型別====
| |
|
| |
| List.map 的型別:<code>('a -> 'b) -> 'a list -> 'b list</code>
| |
|
| |
| <code>let mapped_list = List.map (fun x -> 2 * x) a_list</code>
| |
|
| |
| ====可變變數(reference)====
| |
| <pre>
| |
| let () =
| |
| let r = ref 0 in (* 可變 reference *)
| |
| for i = 0 to 9 do
| |
| r := !r + 1 (* 刷新 r 值,使其 + 1)
| |
| done
| |
|
| |
| </pre>
| |
| 另外還有 record、array、GADT、垃圾回收等等。
| |
|
| |
| ===1.2基本操作===
| |
| <pre>
| |
| let () = print_string "foo" (*回傳 unit*)
| |
| </pre>
| |
| ==== 迴圈====
| |
| <pre>
| |
| let () =
| |
| let r = ref 0 in
| |
| for i = 0 to 9 do
| |
| r := !r +1
| |
| done
| |
| </pre>
| |
|
| |
|
| |
|
| |
| ====部分應用(不需括號)====
| |
|
| |
| int -> (int -> int) 即 int -> int -> int
| |
|
| |
| add 的以下寫法:
| |
| <pre>
| |
| add x y = x + y
| |
| add y = fn x -> x + y
| |
| add = fn x y -> x + y
| |
| </pre>
| |
|
| |
| ====互遞迴====
| |
| <pre>
| |
| let rec f x = ...
| |
| and g x = .....
| |
| </pre>
| |
|
| |
| ====布林 boolean 操作====
| |
|
| |
| 操作子
| |
| <pre>
| |
| && || not
| |
|
| |
| 條件與區塊
| |
| <pre>
| |
| if ... then ... else...
| |
| while ... do ... done
| |
| </pre>
| |
|
| |
| 比對操作子
| |
| <pre>
| |
| == 同一記憶體位置
| |
| = 值相等
| |
| <> 不等於
| |
| </pre>
| |
|
| |
| ====product type====
| |
| add (x, y) = x + y
| |
|
| |
| (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>
| |
|
| |
| ====Bool====
| |
| <pre>type bool = True | False</pre>
| |
| ====List====
| |
| <pre>type 'a list =
| |
| | Nil
| |
| | Cons of 'a * 'a list</pre>
| |
|
| |
| ====coproducts====
| |
| <pre>type ('a, 'b) either =
| |
| | Left of 'a
| |
| | Right of 'b</pre>
| |
|
| |
| ====unit====
| |
| <pre>type unit = | T</pre>
| |
| 只有這個值 <code>()</code>
| |
|
| |
| ====Empty====
| |
| <pre>type Empty = |</pre>
| |
|
| |
| ====Natural Number====
| |
| <pre>type Nat =
| |
| |Zero
| |
| |Suc of nat</pre>
| |
| ====例外處理:Option type and exception====
| |
| 處理例外的方式
| |
|
| |
| Option type
| |
| <pre>type 'a option =
| |
| | Some of 'a
| |
| | None
| |
| </pre>
| |
| exception
| |
| <pre>let hd l =
| |
| match l with
| |
| | x::l -> x
| |
| | [] -> raise Not_found</pre>
| |
|
| |
| 搭配
| |
| <pre>try
| |
| ...
| |
| with
| |
| | Not_found -> ...</pre>
| |
|
| |
|
| [[分類:資訊]] | | [[分類:資訊]] |
| [[分類:邏輯學]] | | [[分類:邏輯學]] |