カリー化

Haskell のひとつ以上の引数をとる関数はカリー化された関数として定義される。

add :: Int -> Int -> Int -> Int
add x y z = x + y + z

add1 :: Int -> (Int -> (Int -> Int))
((add1 x) y) z = x + y + z

add と add1 は同等である。
Int -> Int :Intの引数でIntの値を返す関数。
Int -> (Int -> Int):Intの引数でIntの値を返す関数を返す。

Prelude> :load "func.hs"
[1 of 1] Compiling Main             ( func.hs, interpreted )
Ok, modules loaded: Main.

*Main> :t add
add :: Int -> Int -> Int -> Int
*Main> :t add1
add1 :: Int -> Int -> Int -> Int

引数をひとつ適用された関数は、更にひとつの引数を適用すると値を返す関数を返す。

Main> let f1 = add 1
Main> :t f1
f1 :: Int -> Int -> Int

さらに引数を適用させると、更にひとつの引数をとって値を返す関数を返す。

Main> let f2 = f1 2
Main> :t f2
f2 :: Int -> Int

さらに引数を適用させると、値を返す。

Main> f2 3
6