我无法理解以下example from the NICTA course:
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
答案 0 :(得分:0)
以下推导出现了什么问题?
问题在于这段话:
(:) <$> f :: ((->) a) ([a] -> [a])
函数应用程序始终优先于任何运算符,因此(:) <$> f a
不是((:) <$> f) a
,而是(:) <$> (f a)
,而是(:) <$> :: F a -> F ([a] -> [a]) -- The third line of your derivation.
f :: a -> A b
f a :: A b
(:) <$> f a :: A ([b] -> [b])
。其余部分顺利进行(您可能希望在阅读下面的解决方案之前尝试自己完成):
(<*>) :: A (a -> b) -> A a -> A b
(:) <$> f a :: A ([b] -> [b])
(:) <$> f a <*> :: A [b] -> A [b]
b :: A [b]
(:) <$> f a <*> b :: A [b]
console.log("Looking for " + log_url)
$.ajax({
url: log_url,
dataType: "xml"
})
.done(function(xml) {
console.log("FOUND " + log_url);
})