V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
eote
V2EX  ›  编程

求教等式为啥成立

  •  
  •   eote · May 26, 2022 · 2009 views
    This topic created in 1435 days ago, the information mentioned may be changed or developed.
    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
    
    3 replies    2022-05-27 05:29:21 +08:00
    eote
        2
    eote  
    OP
       May 26, 2022
    @iamzuoxinyu url 说的是 pattern failure 吧
    leomayleomay
        3
    leomayleomay  
       May 27, 2022
    Prelude 里面的 guard 貌似需要 context 是 Alternative https://hackage.haskell.org/package/base-4.16.1.0/docs/Control-Monad.html#v:guard
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2678 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 03:43 · PVG 11:43 · LAX 20:43 · JFK 23:43
    ♥ Do have faith in what you're doing.