by shigemk2

当面は技術的なことしか書かない

部分適用とセクション

Haskellの関数は部分適用を利用している

つまり、関数を本来より少ない引数で呼び出しているのだ。
また、中置関数についても、セクションという機能を使って部分適用することが出来る。
中置関数をセクションするには、片側だけに値を置いて括弧で囲むだけである。
これで引数を1つ、値を置かなかった側に取る関数が出来る。

multThree :: Int -> Int -> Int -> Int
multThree x y z = x * y * z

compareWithHundred :: Int -> Ordering
compareWithHundred x = compare 100 x

divideByTen :: (Floating a) => a -> a
divideByTen = (/10)

結果

Prelude> max 4 5
5
Prelude> (max 4)

<interactive>:3:1:
    No instance for (Show (a0 -> a0))
      arising from a use of `print'
    Possible fix: add an instance declaration for (Show (a0 -> a0))
    In a stmt of an interactive GHCi command: print it
Prelude> (max 4) 5
5
Main> let multTwoWithNine = multThree 9
Main> multTwoWithNine 2 3
54
Main> compareWithHundred 99
GT
Main> divideByTen 200
20.0
Main> 200 / 10
20.0
Main> (/10) 200
20.0
Main> multThree 3 4

<interactive>:17:1:
    No instance for (Show (Int -> Int))
      arising from a use of `print'
    Possible fix: add an instance declaration for (Show (Int -> Int))
    In a stmt of an interactive GHCi command: print it
Main>