we have introduced monoids before, and in this post, we will continue to examine the usage of monoids, we will continue this post with introduction on the some datascture implemented with monoids, such as tree....
in this post we will discuss something that is related to the Monoid with the fold data structure.
One of the more interesting ways to put monoids to work is to make them help us define folds over various data structures.
Fold can work over alomst any data structure, Trees especially lend themeselves well to folding.
In fact, there is a Foldable type class was introduced.
to use that, you can do like this:
import qualified Foldable as F
function that may comes handy are included as follow.
foldr, foldl, foldr1 and foldl1
let's compare the Prelude.Foldr and hte Foldable.foldr,
ghci> :t foldr foldr :: (a -> b -> b) -> b -> [a] -> b ghci> :t F.foldr F.foldr :: (F.Foldable t) => (a -> b -> b) -> b -> t a -> b
as you can see that the foldr in the Prelude is used for the list type, while the foldr is used for almost any types.
some of the test comparision of the two is like this:
ghci> foldr (*) 1 [1,2,3] 6 ghci> F.foldr (*) 1 [1,2,3] 6 ghci> F.foldl (+) 2 (Just 9) 11 ghci> F.foldr (||) False (Just True) True
Now let's make some type part of the Foldable, so that we can do foldx on the customized types. remember the types that we have showed before like this:
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
suppose that we are going to make a visiting procedure that visit the tree, here is the code.
before going to the real code, we will first visit the code which we will use to help us.. It is the FlatMap method.
foldMap :: (Monoid m, Foldable t) => (a -> m) -> t a -> mthis is a function which takes as its first parameter of the type that our foldable stucture contains, and returns a monoid,. and the second type is a foldable structure that contains values of type a. It maps that function over the foldable structure, thus producing a foldable structure that contains monoid values. Then, by doing mappend between those monoid values, it joins them all into a single monoid value.
相关推荐
Atom-haskell-ghc-mod.zip,haskell-ghc-mod atom packagehaskell ghc mod atom包,atom是一个用web技术构建的开源文本编辑器。
Atom-ide-haskell-hoogle.zip,在光标下显示符号的滚动信息艾德·哈斯克尔·胡格尔,atom是一个用web技术构建的开源文本编辑器。
haskell-mode emacs haskell-mode emacs
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...
Programming-in-Haskell-2nd-Edition.pdf
haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...
用于 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 ...
Get Programming with HASKELL-2018-英文版
演示医疗用例的参考DAML应用程序 -Haskell-TypeScript-下载
Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf
haskell-chart, haskell的2D 图表库 图 haskell的2D 图表库进一步的信息可以在关联的 wiki中找到。
A History of Haskell - Being Lazy With Class
haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...
Atom-atom-haskell-scry.zip,De-emphasize qualified Haskell identifiers.SCRY,atom是一个用web技术构建的开源文本编辑器。
Atom-atom-haskell-pointfree.zip,atom包:将选择转换为无点或有点表示Haskell无点包,atom是一个用web技术构建的开源文本编辑器。
Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid 已加书签
Atom-haskell-debug.zip,使用ghci在atom中实现图形haskell调试器Haskell调试,atom是一个用web技术构建的开源文本编辑器。