Hi Rohit It looks like you might be using the parser combinators for Graham Hutton's book. Note that the code in the book does not work with standard Haskell as Graham doesn't want to clutter his presentation with "newtypes". There is an explanation at the end of the chapter and alternative code on the website accompanying the book that is written in standard Haskell. Best wishes Stephen On 17 November 2014 15:20, Rohit Sharma <rohits79@gmail.com> wrote:
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)
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners