[Haskell-beginners] Small question

Alexander Dunlap alexander.dunlap at gmail.com
Sun Feb 28 16:14:52 EST 2010


On Sun, Feb 28, 2010 at 1:06 PM, edgar klerks <edgar.klerks at gmail.com> wrote:
> Hi there,
>
> I have created a function mkc:
>
> mkc :: (a -> b) -> a -> m b
> mkc f = (\x -> return $ f x)
>
> which works wonderful in statements like:
>
> number <- many1 digit >>= mkc(read)
>
> But somehow I think this function should already exist, it is some kind of
> lifting. If it exist where can I find it? Or is there a reason, why it isn't
> in prelude, because it is more elegant way to solve this problem.
>
> Thanks in advance,
>
> Edgar
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>

For something like this, we generally do

fmap read (many1 digit)

fmap is equivalent to liftM from Control.Monad and liftA from
Control.Applicative. It has type Functor f => (a -> b) -> (f a -> f
b). All monads can be functors (and all of the standard ones are), so
it can generally be used as a function (a -> b) -> (m a -> m b) for a
monad m. liftM has the type Monad m => (a -> b) -> (m a -> m b).

Alex

Alex


More information about the Beginners mailing list