(* 目的:リストの各要素に f を施したリストを返す *) (* map : ('a -> 'b) -> 'a list -> 'b list *) let rec map f lst = match lst with [] -> [] | first :: rest -> f first :: map f rest (* 目的:リストの各要素に 1 を加えたリストを返す *) (* g1 : int list -> int list *) let g1 lst = let add1 n = n + 1 in map add1 lst (* テスト *) let test1 = g1 [] = [] let test2 = g1 [1; 2; 3] = [2; 3; 4] let test3 = g1 [3; 2; 5; 4; 1] = [4; 3; 6; 5; 2] (* 目的:リストの各要素を 2 乗したリストを返す *) (* g2 : int list -> int list *) let g2 lst = let square n = n * n in map square lst (* テスト *) let test4 = g2 [] = [] let test5 = g2 [1; 2; 3] = [1; 4; 9] let test6 = g2 [3; 2; 5; 4; 1] = [9; 4; 25; 16; 1]