[Haskell-beginners] Unable to sequence using 'do' operator

Rohit Sharma rohits79 at gmail.com
Mon Nov 17 15:20:09 UTC 2014


type Parser a = String -> [(a, String)]

Hi,

I am learning haskell and wondering why my definition of firstAndThird does
not work with the do operator, however when i try to use the same using
bind (firstandThird2) it works as expected. I basically want to return a
tuple of first and third character of a string. Can someone please correct
me what i might be overseeing?

I have pasted the same code on codetidy in case if the email lose
formatting.
http://codetidy.com/5726/

Many Thanks,
Rohit

zero :: Parser a
zero = \inp -> []

result :: a -> Parser a
result x = \inp -> [(x, inp)]

item :: Parser Char
item = \inp -> case inp of
                   [] -> []
                   (x:xs) -> [(x,xs)]

bind :: Parser a -> (a -> Parser b) -> Parser b
p `bind` f = \inp -> concat [ ((f x) inp') | (x, inp') <- p inp]

sat :: (Char -> Bool) -> Parser Char
sat predicate = item `bind` (\x -> if predicate x then result x else zero )

lower :: Parser Char
lower = sat (\x -> 'a' <= x && x <= 'z')

firstAndThird :: Parser (Char, Char)
firstAndThird = do x <- item
                        item
                   y <- item
                   return (x,y)

firstAndThird2 :: Parser (Char, Char)
firstAndThird2 = item `bind` \x ->
      item `bind` \y ->
      item  `bind` \z ->
      result (x,z)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141117/81b605dc/attachment.html>


More information about the Beginners mailing list