qualcuno può aiutarmi a capire come utilizzare lo stile applicativo per la scrittura di parser parsec? Questo è il codice che ho:Parsec e stile applicativo
module Main where
import Control.Applicative hiding (many)
import Text.Parsec
import Data.Functor.Identity
data Cmd = A | B deriving (Show)
main = do
line <- getContents
putStrLn . show $ parseCmd line
parseCmd :: String -> Either ParseError String
parseCmd input = parse cmdParse "(parser)" input
cmdParse :: Parsec String() String
cmdParse = do
slash <- char '/'
whatever <- many alphaNum
return (slash:whatever)
cmdParse2 :: String -> Parsec String() String
cmdParse2 = (:) <$> (char '/') <*> many alphaNum
ma quando provo a compilare, ottengo seguendo:
/home/tomasherman/Desktop/funinthesun.hs:21:13:
Couldn't match expected type `Parsec String() String'
with actual type `[a0]'
Expected type: a0 -> [a0] -> Parsec String() String
Actual type: a0 -> [a0] -> [a0]
In the first argument of `(<$>)', namely `(:)'
In the first argument of `(<*>)', namely `(:) <$> (char '/')'
Failed, modules loaded: none.
L'idea è che voglio cmdParse2 fare stessa cosa che cmdParse fa, ma utilizzando roba applicativa ... il mio approccio è probabilmente completamente sbagliato, sono nuovo di haskell
Il tuo errore è come se tu avessi scritto '(++) <$> ...' 'non (:) <$> ...'. – huon
scusate, risolto .. lo stavo provando sia con ++ che con: e ho risolto il problema con – Arg
Il massimo in domande ben studiate, perché la risposta è "Ben fatto, scelta del suono, eseguita correttamente, ma correggere questo errore di tipo secondario ". +1 – AndrewC