`

haskell - few more monad - Useful monadic functions

阅读更多

Because the Monad are so lynch point of the haskell language, we will examine more of the monadic functions, the functions that we are going to examine include: 

 

  • operate on monadic values
  • return monadic values as their results (or both!).

 

Such functions are usually referred to as monadic functions. While some of them will be brand new, others will be monadic counterparts of functions that we already know, like filter and foldl. Let's see what they are then!

 

LiftM and their friends..

 due to some historical reasons, Monad was supposed to be made part of the Monad, however, it is not part of  Applicative because Monad was introduced far before the Applicative. 

 

But we don't need to bother on that, because there is a function called LIstM function, which takes another function and a monadic value and maps it over monadic, it could better to illustrate the idea by seeing the definition. 

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

 

and this is the type definition of fmap .. 

fmap :: (Functor f) => (a -> b) -> f a -> f b  

 

learnyouahaskell 写道
If the Functor and Monad instances for a type obey the functor and monad laws, these two amount to the same thing (and all the monads that we've met so far obey both).This is kind of like pure and return do the same thing, only one has anApplicative class constraint whereas the other has a Monad one. Let's try liftM out:

 

and we can tell by the following test. 

ghci> liftM (*3) (Just 8)  
Just 24  
ghci> fmap (*3) (Just 8)  
Just 24  
ghci> runWriter $ liftM not $ Writer (True, "chickpeas")  
(False,"chickpeas")  
ghci> runWriter $ fmap not $ Writer (True, "chickpeas")  
(False,"chickpeas")  
ghci> runState (liftM (+100) pop) [1,2,3,4]  
(101,[2,3,4])  
ghci> runState (fmap (+100) pop) [1,2,3,4]  
(101,[2,3,4])  

 This is how liftM is implemented.  

 

liftM :: (Monad m) => (a -> b) -> m a -> m b  
liftM f m = m >>= (\x -> return (f x))  

 or you can do is with the monad. 

liftM :: (Monad m) => (a -> b) -> m a -> m b  
liftM f m = do  
    x <- m  
    return (f x)  

 

we have seen the Applicative monad, which allow us to functions beween values with contexts as if they were normal values,.

and <$> is just like the fmap in Functor while the type of the <*> is

 

(<*>) :: (Applicative f) => f (a -> b) -> f a -> f b  

 it is very resembles of the liftM (only for the difference that the infix operator and others) . and we do have an equivalent on the Monad space, the function is called "ap", and the definition of the ap mehtod is as follow. 

 

ap :: (Monad m) => m (a -> b) -> m a -> m b  
ap mf m = do  
    f <- mf  
    x <- m  
    return (f x)  

 someimes,  you can interchangably use the ap and the <*> operator, as shown in the following example.s 

 

ghci> Just (+3) <*> Just 4  
Just 7  
ghci> Just (+3) `ap` Just 4  
Just 7  
ghci> [(+1),(+2),(+3)] <*> [10,11]  
[11,12,12,13,13,14]  
ghci> [(+1),(+2),(+3)] `ap` [10,11]  
[11,12,12,13,13,14]  

 

. In fact, many times when a type is found to be a monad, people first write up a Monad instance and then make an Applicative instance by just saying that pure is return and <*> is ap. Similarly, if you already have a Monad instance for something, you can give it a Functor instance just saying that fmap is liftM.

 

liftA2

ther is also an liftA2 function, while instead of just take one paramter into a context, it has two more of the context, and the definition of the liftA2 is like this: 

liftA2 :: (Applicative f) => (a -> b -> c) -> f a -> f b -> f c  
liftA2 f x y = f <$> x <*> y  

 

you may also hve have liftA3, liftA4, and etc.. 

 

In a nutshell, we can see that Monad is stronger than Applicative and functors, and even though all monads are functors and applicative functors, they don't necessarily have Functor and Applicative instances,

 

The join function

 

join gives you the ability to flatten monad values, such as Just (Just 9) can we make that into Just 9? It turned out we can flatten any nested monad. 

 

the definition of the join method is as such .

join :: (Monad m) => m (m a) -> m a  

 e.g. of this nclude the following. 

ghci> join (Just (Just 9))  
Just 9  
ghci> join (Just Nothing)  
Nothing  
ghci> join Nothing  
Nothing  

 and 

ghci> join [[1,2,3],[4,5,6]]  
[1,2,3,4,5,6]  

 as you can see, the join is just concat for list values, and to flattern say a writer value. 

ghci> runWriter $ join (Writer (Writer (1,"aaa"),"bbb"))  
(1,"bbbaaa")  

 it is the mappend method of the Monoid is used. 

 

the implementation of the join is as follow 

join :: (Monad m) => m (m a) -> m a  
join mm = do  
    m <- mm  
    m  

 rather simple, isn't it ? well, the magic within is that it first try to get the result of the monad (mm, where monad is nested), and put the result (which itself is a monad ) back to the return value. For instance, Maybe values result in Just values only if the outer and inner values are both Just values, here what this would look like if the mm value was set in advanced to Just (Just 8)

joinedMaybes :: Maybe Int  
joinedMaybes = do  
    m <- Just (Just 8)  
    m  

 

Savvy?

 

Perhaps the most interesting thing about join is that for every monad, feeding a monadic value to a function with >>= is the same thing as just mapping that function over the value and then using join to flatten the resulting nested monadic value! In other words, m >>= f is always the same thing asjoin (fmap f m)! It makes sense when you think about it. With >>=, we're always thinking about how to feed a monadic value to a function that takes a normal value but returns a monadic value. If we just map that function over the monadic value, we have a monadic value inside a monadic value. For instance, say we haveJust 9 and the function \x -> Just (x+1). If we map this function overJust 9, we're left with Just (Just 10).

 

Last we will see more e.g. on the join functions. 

ghci> runState (join (State $ \s -> (push 10,1:2:s))) [0,0,0]  
((),[10,1,2,0,0,0])  

 and 

ghci> join (Right (Right 9)) :: Either String Int  
Right 9  
ghci> join (Right (Left "error")) :: Either String Int  
Left "error"  

 

filterM

the filerM function is the monadic value of filter, and filter being the bread of Haskell (map being the butter), it takes a predicate and a list to filer out .. 

 

the definition of filter is 

 

filter :: (a -> Bool) -> [a] -> [a]  
 

 

while this works out like a chime, but what if we want to insert some logs such as "Accepting the value"? ..

 

well, monad provides us the ability. and the filterM method is like this: 

filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]  

 so, let's say we want to implement a method which keeps the numbers which is lower than 4, here is the code. 

keepSmall :: Int -> Writer [String] Bool  
keepSmall x  
    | x < 4 = do  
        tell ["Keeping " ++ show x]  
        return True  
    | otherwise = do  
        tell [show x ++ " is too large, throwing it away"]  
        return False  

 

instead of just and returning a Bool, this function returns a Writer [String] Bool, it is a monad value, let's give it a spin (dry run) 

 

ghci> fst $ runWriter $ filterM keepSmall [9,1,5,2,10,3]  
[1,2,3]  

 and if we want to examine the log, here is the code. 

 

ghci> mapM_ putStrLn $ snd $ runWriter $ filterM keepSmall [9,1,5,2,10,3]  
9 is too large, throwing it away  
Keeping 1  
5 is too large, throwing it away  
Keeping 2  
10 is too large, throwing it away  
Keeping 3  

 

 

and another cool trick is using the filterM to get the powerset of a list, (if we think of them as sets ofr now), the powerset of a some set is a set of all subsets of that set. so if we have a set like [1, 2, 3], its powerset would inclde

[1,2,3]  
[1,2]  
[1,3]  
[1]  
[2,3]  
[2]  
[3]  
[] 

 and here is the powerset funciton,. 

 

powerset :: [a] -> [[a]]  
powerset xs = filterM (\x -> [True, False]) xs  

 We choose to drop and keep every element, regardless of what that element is. We have a non-deterministic predicate, so the resulting list will also be a non-deterministic value and will thus be a list of lists. (think of this way, instead of returning a true/false (singular value), we returned a monad, - in this case, the monad value is a list, and the Monad value can be applied to each and every element of the argument list, since the monad value has a List context (with valued contained being Bools, we shall apply the List Monad - which willl have some kind of list generator behaviror and the net result, being the non-deterministic collectoin of sets) ..

 

foldM

the monadic counterparts to foldl is foldM, if you remember your folds from the folds section, you know that foldl takes a binary function, a starting accumulator and a list to fold up and then folds it from the left  into a single vlaue by using the binary function. foldM does the same, except it takes a binary function which produces a monadic value and folds the list up with that.. 

 

the definition of the foldl is like this: 

foldl :: (a -> b -> a) -> a -> [b] -> a  

 

whereas the foldM has the following definition 

foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a  

 

and we will show an example in this example we will see that it does the necesary accumulating, but it will  erturn a fail result if the accumulated number is greater than 9, here is what we have (for a monad accumlator0 

 

binSmalls :: Int -> Int -> Maybe Int  
binSmalls acc x  
    | x > 9     = Nothing  
    | otherwise = Just (acc + x)  

 

now, with that, we can use that to foldM the follownig list. 

ghci> foldM binSmalls 0 [2,8,3,1]  
Just 14  
ghci> foldM binSmalls 0 [2,11,3,1]  
Nothing  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics