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가하는 같은 일을 할 것입니다,하지만 사용이 내가 가지고있는 코드입니다 applicative 물건 ... 내 접근 방식은 아마 완전히 잘못되었습니다, 난 haskell에 처음이에요
귀하의 오류가있다'(++) <$> ...'하지'(:) <$>을 ... '. – huon
죄송합니다, 고정 .. 나는 모두와 함께 노력했다 + + : 그리고 그것을 섞여있어 – Arg
답변은 "음, 잘 선택, 잘 했어,하지만이 사소한 오류를 해결하기 때문에 잘 연구 질문 궁극 . " +1 – AndrewC