Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Very basic question on monads (Chadda? Fouch?) ---------------------------------------------------------------------- Message: 1 Date: Wed, 23 Feb 2011 11:31:06 +0100 From: Chadda? Fouch? <chaddai.fou...@gmail.com> Subject: Re: [Haskell-beginners] Very basic question on monads To: black...@pro-ns.net Cc: beginners@haskell.org Message-ID: <aanlktiki3lsvk4pn_txncauqxfsvaqwutm1srup-8...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Wed, Feb 23, 2011 at 6:30 AM, <black...@pro-ns.net> wrote: > I'm working through the "Write Yourself a Scheme" wikibook and having > trouble with one of the exercises. ?For the function parseNumber :: Parser > LispVal, both > > parseNumber = liftM (Number . read) $ many1 digit > parseNumber' = do digits <- many1 digit > ? ? ? ? ? ? ? ? return $ (Number . read) digits > > work. ?But > > parseNumber'' = many1 digit >>= liftM read >>= liftM Number You misunderstand liftM : liftM takes an ordinary function (a -> b) and "lift" it so that it acts inside the monad and become (m a -> m b) thus the parameter of "liftM f" must be a monadic action but through (>>=) you feed it an ordinary value since (>>=) bind a monadic action (m a) to a function that takes an ordinary value and return an action (a -> m b). You used liftM as if it was (return .) The correct usage would have been : > parseNumber'' = liftM (Number . read) (many1 digit) like your parseNumber or (using Applicative if you wish to do so) : > parseNumber'' = Number . read <$> many1 digit -- Jeda? ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 32, Issue 43 *****************************************