
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

On Sun, Feb 28, 2010 at 1:06 PM, edgar klerks
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@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

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.
participants (3)
-
Alexander Dunlap
-
edgar klerks
-
Felipe Lessa