Ejemplos de transformadores aplicativos Haskell
La wiki en www.haskell.org nos dice lo siguiente sobre los Transformadores Aplicativos:
Entonces, ¿dónde están los transformadores aplicativos? La respuesta es que no necesitamos transformadores especiales para los funtores aplicativos, ya que se pueden combinar de forma genérica.http://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers
Intenté lo siguiente para tratar de combinar un montón de funtores aplicativos. Pero todo lo que conseguí fue un montón de errores. Aquí está el 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"]
Esto produce muchos errores tipográficos, que aunque puedo entender parcialmente, no pude resolverlos en absoluto.
Los errores se dan al final.
Entonces, ¿cómo puedo combinar el Applicative Maybe y el Applicative de la lista, por ejemplo?
¿Cómo combino el aplicativo estatal y el aplicativo de lista, por ejemplo? ¿Hay otros ejemplos, digamos, combinando Tal vez y Lista, Tal vez y Estado y, finalmente, el terrible de todos los aplicativos de OI y el Estado?
Gracias.
Los mensajes de error de GHCi siguen.
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>