Parsecパーサーを書くためのApplicativeスタイルの使い方を教えてもらえますか?ParsecとApplicativeスタイル
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
をしかし、私はそれをコンパイルしようとすると、私は以下の取得:
/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.
アイデアは私がcmdParse2がcmdParseと同じことをしたいということですが、使用してこれは私が持っているコードです。アプリケーションのもの...私のアプローチはおそらく完全に間違っている、私はhaskellに新しいです
あなたの誤差がある '(++)<$> ...' 'ない(:) <$>を...'。 – huon
申し訳ありませんが、私は++との両方で試していました:そしてそれを混ぜ合わせました – Arg
答えが「うまくいきました、健全な選択、正しく行われましたが、このマイナータイプのエラーを修正するので、 " +1 – AndrewC