【发布时间】:2014-11-10 10:56:51
【问题描述】:
我无法理解以下 Applicative 实例。有人可以解释一下 Applicative 做什么(在这种情况下)以及如何使用它吗?或者写得少一些混淆?谢谢!
newtype Parser a = P { getParser :: String -> Maybe (a, String) }
instance Applicative Parser where
pure = success
P p <*> P p' = P $ \s -> case p s of
Just (f, s') -> fmap (applyToFirst f) $ p' s'
Nothing -> Nothing
{-|
Applies a function to the first component of a pair.
-}
applyToFirst :: (a -> b) -> (a, c) -> (b, c)
applyToFirst f (x, y) = (f x, y)
【问题讨论】:
-
Getting started with Haskell 的可能重复项
-
您编写的代码中“混淆”了什么?很清楚。
-
@BartekBanachewicz:我不明白它是如何复制的。
-
@TomEllis 我几乎看不出在 SO 上解释
Applicative的每一个可能实例的价值。
标签: haskell