OCaml

練習問題 4.1 uncurry関数をつくる

練習問題 4.1

以下の関数 curry は、与えられた関数をカリー化する高階関数です。

let curry f x y = f (x, y);;
val curry : (‘a * ‘b -> ‘c) -> ‘a -> ‘b -> ‘c =
let average (x, y) = (x +. y) /. 2.0;;
val average : float * float -> float =
let curried_avg = curry average;;
val curried_avg : float -> float -> float =
average (4.0, 5.3);;
: float = 4.65
curried_avg 4.0 5.3;;
: float = 4.65

この逆、つまり(2引数の)カリー化関数を受け取り、二つ組を受け取る関数に変換する uncurry 関数を定義しなさい。

uncurry;;
: (‘a -> ‘b -> ‘c) -> ‘a * ‘b -> ‘c =
let avg = uncurry curried_avg in
avg (4.0, 5.3);;
: float = 4.65

(* 練習問題 4.1 *)

let curry f x y = f (x, y);;
let average (x, y) = (x +. y) /. 2.0;;
let curried_avg = curry average;;

let test1 = average (4.0, 5.3) = 4.65;;
let test2 = curried_avg 4.0 5.3 = 4.65;;

let uncurry f (x, y) = f x y;;

let avg = uncurry curried_avg in
avg (4.0, 5.3);;