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.
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)