Beispiele für anwendbare Haskell-Transformatoren

Das Wiki auf www.haskell.org enthält folgende Informationen zu anwendbaren Transformatoren:

Wo sind also anwendbare Transformatoren? Die Antwort ist, dass wir keine speziellen Transformatoren für anwendungsbezogene Funktoren benötigen, da sie auf generische Weise kombiniert werden können.http://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers

Ich habe Folgendes versucht, um eine Reihe von anwendbaren Funktoren zu kombinieren. Aber alles, was ich bekam, war eine Menge Fehler. Hier ist der Code:

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"]

Dies führt zu einer Reihe von Tippfehlern, die ich zwar teilweise verstehen, aber überhaupt nicht beheben konnte.

Die Fehler werden am Ende angegeben.

Wie kombiniere ich also zum Beispiel den Vielleicht-Antrag und den Listenantrag?

Wie kombiniere ich zum Beispiel den staatlichen Antragsteller und den Listenantragsteller? Gibt es noch andere Beispiele, sagen wir, die Vielleicht und Liste, Vielleicht und Staat und schließlich das Schreckliche aller IO- und Staatsbewerber kombinieren?

Vielen Dank.

Es folgen die GHCi-Fehlermeldungen.

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>

Antworten auf die Frage(4)

Ihre Antwort auf die Frage