The liftM function includes a function that receives input and outputs the result to a function that receives input in some monad and outputs in the same monad. Let's look at its definition:
liftM :: Monad m => (a -> b) -> ma -> mb liftM f mx = mx >>= \x -> return (fx)
Haskell strings are lists of characters ( type String = [Char] ), so
"abc" = ['a', 'b', 'c'] :: [Char]
From your application compiler, a = Char , b = [Char] , ma = [Char] , m = [] . So mb = [[Char]] = [String] . A list is a monad, where return x = [x] and (>>=) = concatMap . Therefore, if we specialize above the definition, we get:
liftM f mx = concatMap (\x -> [fx]) mx
And if we apply the arguments, we get:
concatMap (\x -> [[x]]) ['a', 'b', 'c'] = concat $ map (\x -> [[x]]) ['a', 'b', 'c'] = concat $ [[['a']], [['b']], [['c']]] = [['a'], ['b'], ['c']] = ["a", "b", "c"]
aemxdp
source share