我无法理解NICTA 课程中的以下示例:
instance Traversable List where
traverse ::
Applicative f =>
(a -> f b)
-> List a
-> f (List b)
traverse f =
foldRight (\a b -> (:.) <$> f a <*> b) (pure Nil)
在下文中,我将替换List
为[]
,因此我们有:
instance Traversable [] where
traverse ::
Applicative f =>
(a -> f b)
-> [a]
-> f ([b])
traverse f =
foldRight (\a b -> (:) <$> f a <*> b) []
特别是,以下类型的推导有什么问题(<*>)
?
(:) :: a -> [a] -> [a]
(<$>) :: a -> b -> F a -> F b, F for Functor
(所以...)
(:) <$> :: F a -> F ([a] -> [a])
f :: a -> A b :: ((->) a) (A b), A for Applicative
(所以...)
(:) <$> f :: ((->) a) ([a] -> [a])
(:) <$> f a :: [a] -> [a]
(<*>) :: A (a -> b) -> A a -> A b