我一直在研究问题67A of 99 Haskell Questions。现在的问题是给定字符串构建树:"x(y,a(,b))" => Branch 'x' (Branch 'y' Empty Empty) (Branch 'a' Empty (Branch 'b' Empty Empty))
一种解决方案使用Parsec
是如下:在GHCi中找不到Parsec模块
import Text.Parsec.String
import Text.Parsec hiding (Empty)
pTree :: Parser (Tree Char)
pTree = do
pBranch <|> pEmpty
pBranch = do
a <- letter
char '('
t0 <- pTree
char ','
t1 <- pTree
char ')'
return $ Branch a t0 t1
pEmpty =
return Empty
stringToTree str =
case parse pTree "" str of
Right t -> t
Left e -> error (show e)
但是,我GHCI既不能找到Text.Parsec.String
也不Text.Parsec
。这些模块是否过时了? 我GHCI版本6.12.3是
thx,安装parsec-3后编译好。但是,现在运行它时会出现一个'意外的','期待的'(“'异常' – manuzhang 2012-01-30 04:58:14
)输入”x(y,a(,b))“不能由给定的解析器解析, x(y(,),a(,b(,)))“ – 2012-01-30 05:55:41
thx再次,我在Haskell的99个问题上发布你的解决方案,那好吗?如果不是,我会立即删除它 – manuzhang 2012-01-30 08:49:17