Real World Haskell - 第4章 Part11 Code reuse through composition

Real World HaskellHaskell を継続的に勉強中。


Chapter 4. Functional programming
Chapter 4. Functional programming

Code reuse through composition

  • 関数合成は . でできる
suffixes2 xs = init (tails xs)
compose :: (b -> c) -> (a -> b) -> a -> c
compose f g x = f (g x)
suffixes3 xs = compose init tails xs
suffixes4 = compose init tails
ghci> :type (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
suffixes5 = init . tails

こんな風にどんどん連結できる。

capCount = length . filter (isUpper . head) . words

unsafe な partial function を合成する場合は注意が必要。