発展: 順列表現
まんま
module Main where import Text.ParserCombinators.Parsec --import Text.ParserCombinators.Parsec.Char import Text.ParserCombinators.Parsec.Perm perm0 = permute (f (<$$>) char 'a' (<||>) char 'b' (<||>) char 'c') where f a b c = [a,b,c]
で、
Prelude> :l 20080928_parsec00.hs Compiling Main ( 20080928_parsec00.hs, interpreted ) 20080928_parsec00.hs:8: Couldn't match `[t]' against `t1 -> t2' Expected type: [t] Inferred type: t1 -> t2 Probable cause: `f' is applied to too many arguments in the call (f (<$$>) char 'a' (<||>) char 'b' (<||>) char 'c') In the first argument of `permute', namely `(f (<$$>) char 'a' (<||>) char 'b' (<||>) char 'c')' Failed, modules loaded: none.
?
まんま
module Main where import Text.ParserCombinators.Parsec --import Text.ParserCombinators.Parsec.Char import Text.ParserCombinators.Parsec.Perm perm1 :: Parser (String,Char,Char) perm1 = permute (tuple <$?> ("",many1 (char 'a')) <||> char 'b' <|?> ('_',char 'c')) where tuple a b c = (a,b,c) run :: Show a => Parser a -> String -> IO () run p input = case (parse p "" input) of Left err -> do{ putStr "parse error at " ; print err } Right x -> print x
で、
*Main> run perm1 "caaaaab" ("aaaaa",'b','c') *Main> run perm1 "cb" ("",'b','c') *Main> run perm1 "b" ("",'b','_') *Main> run perm1 "" parse error at (line 1, column 1): unexpected end of input expecting "c", "b" or "a" *Main> run perm1 "c" parse error at (line 1, column 2): unexpected end of input expecting "b" or "a" *Main> run perm1 "ca" parse error at (line 1, column 3): unexpected end of input expecting "a" or "b"