[Haskell-beginners] Small question

Felipe Lessa felipe.lessa at gmail.com
Sun Feb 28 16:16:54 EST 2010


On Sun, Feb 28, 2010 at 10:06:06PM +0100, edgar klerks wrote:
> I have created a function mkc:
>
> mkc :: (a -> b) -> a -> m b
> mkc f = (\x -> return $ f x)

Actually the type is

  mkc :: Monad m => (a -> b) -> a -> m b

and you could define it as

  mkc = (return .)

However, we usually don't need this function.

> which works wonderful in statements like:
>
> number <- many1 digit >>= mkc(read)

We prefer to right

  number <- fmap read (many1 digit)

or, even better yet

  import Control.Applicative
  number <- read <$> many1 digit

Note that

  (<$>) :: Functor f => (a -> b) -> (f a -> f b)
  f <$> x = fmap f x

Cheers,

--
Felipe.


More information about the Beginners mailing list