Exemplos de Transformadores Aplicáveis Haskell
O wiki em www.haskell.org nos diz o seguinte sobre Transformadores Aplicativos:
Então onde estão os transformadores de aplicativos? A resposta é que não precisamos de transformadores especiais para functores aplicativos, pois eles podem ser combinados de maneira genérica.http://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers
Eu tentei o seguinte, a fim de tentar combinar um monte de functores aplicativos. Mas tudo o que consegui foi monte de erros. Aqui está o código:
import Control.Applicative
import System.IO
ex x y = (:) <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"]
gt; 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"]
Isso produz muitos erros de tipo, que, embora eu possa entender parcialmente, não consegui resolvê-los.
Os erros são dados no final.
Então, como eu combino o Applicative Maybe e o List Applicative, por exemplo?
Como eu combino o Applicative do estado e o Applicative da lista por exemplo? Há outros exemplos, digamos, combinando Maybe e List, Maybe e State e, finalmente, o terrível de todos os aplicativos IO e State?
Obrigado.
As mensagens de erro do GHCi seguem.
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>