[Haskell-cafe] Confusing `bind` and (>>=)

mirone saul-mirone at qq.com
Tue Oct 10 11:34:16 UTC 2017


Hi there,
  I'm learning haskell for weeks and I'm trying to write a parser using haskell.
  First I create a datatype:
    newtype Parser a = P (String -> [(a, String)])
  I create a function called `bind` :
    bind :: Parser a -> ( a -> Parser b) -> Parser b
    bind f g = P $ \s -> concatMap (\(a, s') -> parse (g a) s') $ parse f s
  and then:
    instance Monad Parser where
      (>>=)  = bind
  that's looks cool, than I write a function
    liftToken :: (Char -> [a]) -> Parser a
    liftToken f = P g where
      g [] = []
      g (c:cs) = f c >>= (\x -> return (x, cs))
  It works well, then I change this function to
    liftToken :: (Char -> [a]) -> Parser a
    liftToken f = P g where
      g [] = []
      g (c:cs) = f c `bind` (\x -> return (x, cs))

  GHC throw errors:
    regular.hs|153 col 14 error| • Couldn't match expected type ‘Parser t0’ with actual type ‘[a]’ • In the first argument of ‘bind’, namely ‘f c’ In the expression: f c `bind` (\ x -> return (x, cs)) In an equation for ‘g’: g (c : cs) = f c `bind` (\ x -> return (x, cs)) • Relevant bindings include f :: Char -> [a] (bound at regular.hs:151:11) liftToken :: (Char -> [a]) -> Parser a (bound at regular.hs:151:1)


  That's really confusing, why I can use (>>=) but I can't use `bind`? Is there any difference between them?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20171010/e7ab9ed8/attachment.html>


More information about the Haskell-Cafe mailing list