10

Applicative スタイルを使用して Parsec パーサーを作成する方法を理解するのを手伝ってくれる人はいますか? これは私が持っているコードです:

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は初めてです

4

2 に答える 2

5

アプリケーションの使用法は適切です。署名が間違っているだけです。試す:

cmdParse2 :: Parsec String () String
于 2012-10-25T09:26:39.387 に答える
4

あなたのアプローチは私には正しいように見えますが、問題はcmdParse2タイプが間違っていることです。と同じタイプである必要がありcmdParseます。ちなみに、char '/'applicative スタイル パーサーでは括弧を省略できます。

于 2012-10-25T09:24:27.403 に答える