この例を考えてみましょう(https://codereview.stackexchange.com/questions/23456/crtitique-my-haskell-function-capitalizeから):
import Data.Char
capWord [] = []
capWord (h:t) = toUpper h : map toLower t
capitalize = unwords . map capWord . words
たとえば、「前後の」変換を抽象化するための良い方法はありunwords . f . words
ますか?私が思いついた最高のものは
class Lift a b | a -> b where
up :: a -> b
down :: b -> a
instance Lift String [String] where
up = words
down = unwords
lifted :: (Lift a b) => (b -> b) -> a -> a
lifted f = down . f . up
capitalize = lifted (map capWord)
しかし、それはあまり柔軟ではなく、、、、および-が必要MultiParamTypeClasses
です。FunctionalDependencies
これは、それがわずかに上を超えていることを示している可能性があります。TypeSynonymInstances
FlexibleInstances