Functor, Applicative, Foldable, Traversalの練習

class Functor f => Applicative f where
  pure :: a->f a
  <*> :: f (a->b)->f a->f b

class Foldable t where
  foldr :: (a->b->b)->b->t a->b
  foldr f b = foldMap (`f` b)
  foldMap :: Monoid m => (a->m)->t a->m
  foldMap f = foldr (mappend . f) mempty

class (Functor t, Foldable t) => Traversable t where
  traverse :: (Applicative f) => (a -> f b) -> t a -> f (t b)
  sequenceA :: Applicative f => t (f a)->f (t a)

class Functor f where
  fmap :: (a->b)->f a->f b

instance Functor [] where
  fmap g x:xs = g x:g xs

instance Traversable List where
  sequenceA [] = pure []
  sequenceA x:xs = (:) <$> x <*> sequenceA xs

  traverse g [] = pure []
  traverse g x:xs = fmap (:) (g x) <*> traverse g xs
(:) :: a->[a]->[a]

(a->b)->f a->f b

(+1) :: Int->Int
fmap (+1) :: f Int->f Int
read :: String->Int
fmap (+1) read :: String->Int

((:[]).(+1)) :: Int->[Int]
fmap ((:[]).(+1)) :: f Int->f [Int]
fmap ((:[]).(+1)) read ::