newtype Parser a = Parser (String -> [(a, String)])
apply :: Parser a -> String -> [(a, String)]
apply (Parser p) s = p s
instance Monad Parser where
return x = Parser (\s -> [(x, s)])
p >>= q = Parser (\s -> [(y, s'') | (x, s') <- apply p s, (y, s'') <- apply (q x) s'])
fail = Parser (\s -> [])
guard :: Bool -> Parser ()
guard True = return ()
guard False = fail
sat :: (Char -> Bool) -> Parser Char
sat p = do { c <- getc; guard (p c); return c }
-- 以上是背景
-- 以下是问题
-- 为啥 guard (p c) >> return c = fail >> return c = fail
-- p c == True 很好理解 guard (p c)结果被弃掉了
-- p c == False 为啥没走到 return c
1
iamzuoxinyu 2022-05-26 14:57:41 +08:00
|
2
eote OP @iamzuoxinyu url 说的是 pattern failure 吧
|
3
leomayleomay 2022-05-27 05:29:21 +08:00
Prelude 里面的 guard 貌似需要 context 是 Alternative https://hackage.haskell.org/package/base-4.16.1.0/docs/Control-Monad.html#v:guard
|