Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
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? <[email protected]>
Subject: Re: [Haskell-beginners] Very basic question on monads
To: [email protected]
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Wed, Feb 23, 2011 at 6:30 AM, <[email protected]> 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
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 32, Issue 43
*****************************************