練習問題 5.4

f, g を適当な型の関数とします。map f (map g l) を map を一度しか使用しない同じ意味の式に書き換えなさい。map (fun x -> … ) l の … の部分はどうなるでしょう?

まず、map 関数を定義する。

let rec map g l =
match l with
[] -> []
| v :: rest ->
(g v) :: map g rest;;

g 関数を (fun x -> x * 2) とすると、

let test1 = map (fun x -> x * 2) [1; 2; 3] = [2; 4; 6];;

となる。

また、上記の問題より

f — (fun x -> x * x)
g — (fun x -> x * 2)

とすると、

# map (fun x -> x * x) (map (fun x -> x * 2) [1; 2; 3]);;

は、

– : int list = [4; 16; 36]

となる。

map を一度だけ使う式にすると、

map (fun x -> (x * 2) * (x * 2)) [1; 2; 3];;

となる。

最終的には、以下のように表すことができる。

let f x = x * x;;
let g x = x * 2;;

let test11 = f 3 = 9;;
let test12 = g 3 = 6;;

map (fun x -> f (g x) ) [1; 2; 3];;


解答

map (fun x -> f (g x)) l

解答のまとめ

let rec map g l =
    match l with
    [] -> []
    | v :: rest ->
            (g v) :: map g rest;;

let test1 = map (fun x -> x * 2) [1; 2; 3] = [2; 4; 6];;

map (fun x -> x * x) (map (fun x -> x * 2) [1; 2; 3]);;

(*
let rec map (f g) l =
    match l with 
    [] -> []
    | v :: rest ->
            (f g) v :: (map (f g) rest);;
*)

map (fun x -> (x * 2) * (x * 2)) [1; 2; 3];;

let f x = x * x;;
let g x = x * 2;;

let test11 = f 3 = 9;;
let test12 = g 3 = 6;;

map (fun x -> f (g x) ) [1; 2; 3];;