categoryML

複数の値を返す関数

trackback1  comment0
プログラミング覚書 ML-009

複数の値を返す関数
----------------------------------------------------------------------

- fun bar(x, y) =
= if x = y then (0, 0)
= else if x > y then (1, x - y)
= else (~1, y - x);
val bar = fn : int * int -> int * int
- bar( 20, 10);
val it = (1,10) : int * int
- bar( 10, 20);
val it = (~1,10) : int * int
- bar( 10, 10);
val it = (0,0) : int * int

----------------------------------------------------------------------
categoryML

複数の引数

trackback1  comment0
プログラミング覚書 ML-008

複数の引数
----------------------------------------------------------------------

- fun f(x, y) = 3 * x + 4 * y;
val f = fn : int * int -> int
- f(2, 3);
val it = 18 : int

----------------------------------------------------------------------
categoryML

リスト

trackback1  comment0
プログラミング覚書 ML-007

リスト
----------------------------------------------------------------------

- val a = [1, 2, 3, 4];
val a = [1,2,3,4] : int list
- val b = ["abc", "def", "ghi"];
val b = ["abc","def","ghi"] : string list
- a;
val it = [1,2,3,4] : int list
- hd( a );
val it = 1 : int
- tl( a );
val it = [2,3,4] : int list
- val c = 0 :: a;
val c = [0,1,2,3,4] : int list
- val d= [1, 2, 3] @ [4, 5, 6];
val d = [1,2,3,4,5,6] : int list

----------------------------------------------------------------------
categoryML

タプル tuple 組

trackback1  comment0
プログラミング覚書 ML-006

タプル tuple 組
----------------------------------------------------------------------

val a = (1,2) : int * int
- val b = (10, 89.7);
val b = (10,89.7) : int * real
- val c = ("hello", 45.6, 12);
val c = ("hello",45.6,12) : string * real * int
- #2(a);
val it = 2 : int
- #1(c);
val it = "hello" : string

----------------------------------------------------------------------
categoryML

if

trackback0  comment0
プログラミング覚書 ML-005

if
----------------------------------------------------------------------

- if 1 < 2 then 3 * 4 else 5 * 6;
val it = 12 : int
- if 1 > 2 then 3 * 4 else 5 * 6;
val it = 30 : int

----------------------------------------------------------------------
categoryML

関数定義

trackback1  comment0
プログラミング覚書 ML-004

関数定義
----------------------------------------------------------------------

- fun NiBai x = x * 2;
val NiBai = fn : int -> int
- NiBai 20;
val it = 40 : int
- NiBai( 30 );
val it = 60 : int

----------------------------------------------------------------------

categoryML

size関数

trackback1  comment0
プログラミング覚書 ML-003

size関数
----------------------------------------------------------------------

- size;
val it = fn : string -> int

----------------------------------------------------------------------
categoryML

^:文字の連結

trackback0  comment0
プログラミング覚書 ML-002

^:文字の連結
----------------------------------------------------------------------

- 23.5 + 45.6;
val it = 69.1 : real
- val test = "Hello";
val test = "Hello" : string
- val aisatu = test ^ " " ^ "World!";
val aisatu = "Hello World!" : string
- size aisatu;
val it = 12 : int
- aisatu;
val it = "Hello World!" : string

----------------------------------------------------------------------
categoryML

型推論

trackback0  comment0
プログラミング覚書 ML-001

型推論
----------------------------------------------------------------------

D:\pd>sml
Standard ML of New Jersey v110.72
[built: Fri Feb :57:49 2010]
- 10 + 3;
val it = 13 : int

----------------------------------------------------------------------
New «‡Top‡» Old