[1; 2; 3]
[]
[2; 3]
[2; 3]
head :: tail
[1; 2; 3] 1 :: [2; 3] 1 :: 2 :: [3] 1 :: 2 :: 3 :: []
int list
foo
foo list
'a list
'a list
'a list
List
length
List.length
List.lenght
List.lenght
List.length: 'リスト-> int
struct
struct
struct pair_of_ints { int a、b; };
(3,4)
int * int
int * string * char
タイプpair_of_ints = {a:int; b:int} ;;
{a = 3; b = 5}
#type pair_of_ints = {a:int; b:int} ;; タイプpair_of_ints = {a:int; b:int; } #{a = 3; b = 5} ;; -:pair_of_ints = {a = 3; b = 5} #{a = 3} ;; 一部のレコードフィールドラベルは未定義です:b
struct foo { int型; #define TYPE_INT 1 #define TYPE_PAIR_OF_INTS 2 #define TYPE_STRING 3 ユニオン{ int i; // type == TYPE_INTの場合。 intペア[2]; // type == TYPE_PAIR_OF_INTSの場合。 char * str; // type == TYPE_STRINGの場合。 } u; };
ui
enum
type
タイプfoo = Nothing | intのint | int * intのペア| 文字列の文字列;;
of part
Nothing
なし Int 3 ペア(4、5) ストリング「hello」 &c。
foo
of
of
列挙記号{正、ゼロ、負};
タイプ記号=正| ゼロ| 負の;;
タイプbinary_tree = intのリーフ| binary_tree * binary_treeのツリー;;
葉3 ツリー(リーフ3、リーフ4) ツリー(ツリー(リーフ3、リーフ4)、リーフ5) ツリー(ツリー(リーフ3、リーフ4)、ツリー(ツリー(リーフ3、リーフ4)、リーフ5))
タイプ「a binary_tree = Leaf of」a | 'a binary_tree *'のツリーbinary_tree ;;
int binary_tree
string binary_tree
string binary_tree
#リーフ "hello" ;; -:string binary_tree = Leaf "hello" #リーフ3.0 ;; -:float binary_tree = Leaf 3。
int list
a' list
タイプ 'a list = [] | :: 'a *'リスト(*これは実際のOCamlではありません*コード)
#type 'a list = Nil | :: 'a *'リスト;; タイプ 'a list = Nil | ::「a *」リスト #なし;; -: 'a list = Nil #1 ::なし;; -:int list = ::(1、Nil) #1 :: 2 :: Nil ;; -:int list = ::(1、::(2、Nil))
[1; 2; 3]
[1; 2; 3]
[1; 2; 3]
1 :: 2 :: 3 :: []
a' list
OCamlの名前 | タイプ定義の例 | 使用例 |
---|---|---|
リスト | intリスト | [1; 2; 3] |
タプル | int *文字列 | (3、「こんにちは」) |
記録する | タイプペア= {a:int; b:文字列} | {a = 3; b = "hello"} |
異形 | タイプfoo = intのint
| int *文字列のペア | Int 3 |
異形 | タイプ記号=正| ゼロ
| 負 | ポジティブ
ゼロ |
パラメータ化された
異形 | タイプ 'a my_list =空
| 'a *'の短所my_list | 短所(1、短所(2、空)) |
タイプexpr =プラスexpr * expr(*はa + b *を意味します) | expr * exprのマイナス(*はa-b *を意味します) | expr * exprの時間(*はa * b *を意味します) | expr * exprの除算(*はa / bを意味します*) | 文字列の値(* "x"、 "y"、 "n"など。*) ;;
時間(値 "n"、プラス(値 "x"、値 "y"))
n * (x+y)
(Value "n", Plus (Value "x", Value "y"))
let rec_string e = eと一致 プラス(左、右)-> "(" ^(to_string left)^ "+" ^(to_string right)^ ")" | マイナス(左、右)-> "(" ^(to_string left)^ "-" ^(to_string right)^ ")" | 時間(左、右)-> "(" ^(to_string left)^ "*" ^(to_string right)^ ")" | 分割(左、右)-> "(" ^(to_string left)^ "/" ^(to_string right)^ ")" | 値v-> v ;; let print_expr e = print_endline(to_string e);;
^
#print_expr(Times(値 "n"、Plus(値 "x"、値 "y")));; (n *(x + y))
オブジェクトを一致させる パターン->結果 | パターン->結果 ...
to_string
to_string
n * (x + y)
(x + y) * n
let recマルチプルアウトe = eと一致 時間(e1、プラス(e2、e3))-> プラス(時間(multiply_out e1、multiply_out e2)、 時間(multiply_out e1、multiply_out e3)) | 時間(プラス(e1、e2)、e3)-> プラス(時間(multiply_out e1、multiply_out e3)、 時間(multiply_out e2、multiply_out e3)) | プラス(左、右)->プラス(左にmultiply_out、右にmultiply_out) | マイナス(左、右)->マイナス(左にmultiply_out、右にmultiply_out) | 時間(左、右)->時間(左に乗算_アウト、右に乗算_アウト) | 分割(左、右)->分割(multiply_out左、multiply_out右) | 値v->値v ;;
#print_expr(multiply_out(Times(値 "n"、Plus(値 "x"、値 "y")));; ((n * x)+(n * y))
Time (e1, Plus (e2, e3))
e1 * (e2 + e3)
(e1 * e2) + (e1 * e3)
(e1 + e2) * e3
multiply_out
e -> e
因数分解e = eと一致 Plus(Times(e1、e2)、Times(e3、e4))e1 = e3-> Times(e1、Plus(e2、e4))の場合 | Plus(Times(e1、e2)、Times(e3、e4))e2 = e4-> Times(Plus(e1、e3)、e4) | e-> e ;;
#factorize(Plus(Times(値 "n"、値 "x")、Times(値 "n"、値 "y"));; -:expr = Times(値 "n"、プラス(値 "x"、値 "y"))
when
when
オブジェクトを一致させる パターン[when条件]->結果 パターン[when条件]->結果 ...
=
Product
expr
タイプexpr =プラスexpr * expr(*はa + b *を意味します) | expr * exprのマイナス(*はa-b *を意味します) | expr * exprの時間(*はa * b *を意味します) | expr * exprの除算(*はa / bを意味します*) | exprリストの積(*はa * b * c * ... *を意味します) | 文字列の値(* "x"、 "y"、 "n"など。*) ;;
警告:このパターンマッチングは完全ではありません。 一致しない値の例を次に示します。 製品_