www.haskell.org 上的 wiki 告诉我们以下有关 Applicative Transformers 的信息:
那么应用变压器在哪里呢?答案是,我们不需要针对应用函子的特殊转换器,因为它们可以以通用方式组合。 http://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers
我尝试了以下方法来尝试组合一堆应用函子。但我得到的只是一堆错误。这是代码:
import Control.Applicative
import System.IO
ex x y = (:) <$> x <*> y
test1 = ex "abc" ["pqr", "xyz"] -- only this works correctly as expected
test2 = ex "abc" [Just "pqr", Just "xyz"]
test3 = ex "abc" (Just "pqr")
test4 = ex (Just 'a') ["pqr", "xyz"]
test5 = ex (return ("abc"):: IO ()) [Just "pqr", Just "xyz"]
这会产生很多类型错误,虽然我可以部分理解,但我根本无法解决它们。
错误在最后给出。
那么,例如,我如何结合 Maybe Applicative 和 List Applicative 呢?
例如,如何将 State Applicative 和 List Applicative 结合起来?还有其他例子吗,比如说,结合了 Maybe 和 List,Maybe 和 State,最后是可怕的 IO 和 State 应用程序?
谢谢。
GHCi 错误消息如下。
example.hs:6:19:
Couldn't match expected type `[Char]' with actual type `Maybe a0'
In the return type of a call of `Just'
In the expression: Just "pqr"
In the second argument of `ex', namely `[Just "pqr", Just "xyz"]'
example.hs:7:19:
Couldn't match expected type `[[Char]]' with actual type `Maybe a0'
In the return type of a call of `Just'
In the second argument of `ex', namely `(Just "pqr")'
In the expression: ex "abc" (Just "pqr")
example.hs:8:23:
Couldn't match expected type `Maybe' with actual type `[]'
In the second argument of `ex', namely `["pqr", "xyz"]'
In the expression: ex (Just 'a') ["pqr", "xyz"]
In an equation for `test4': test4 = ex (Just 'a') ["pqr", "xyz"]
example.hs:9:21:
Couldn't match expected type `()' with actual type `[Char]'
In the first argument of `return', namely `("abc")'
In the first argument of `ex', namely `(return ("abc") :: IO ())'
In the expression:
ex (return ("abc") :: IO ()) [Just "pqr", Just "xyz"]
Failed, modules loaded: none.
Prelude>