`

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  

 

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    haskell-brainfuck:Haskel 脑残翻译

    你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid

    ### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...

    函数式编程-haskell-to-java

    ### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

    haskell-lsp-client:haskell-lsp的客户端库

    haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...

Global site tag (gtag.js) - Google Analytics