Fist we shall answer the question of what is Monad, monad are just beefed up applicative functors, much like applicative functors are only beefed up functors.
so, let's recall monad, and its predecessor, the fmap is like this:
fmap :: (Functor f) => (a -> b) -> f a -> f b
and then we introduced the applica
oi tive , which is a special form of Functor, that takes binary operator and with additional helper function to help chain and organize the function composition. here is the code.
(<*>) :: (Applicative f) => f (a -> b) -> f a -> f b
we will ignore the code which does the funcion chaining and others, Monad is a Monads are a natural extension of applicative functors and with them we're concerned with this:
(>>=) :: (Monad m) => m a -> (a -> m b) -> m b
which means, with a value in a context, ma, and a function which takes a value and return a context with another type b , it gives you back a value of b in the context.
so , in another word, If we have a fancy value and a function that takes a normal value but returns a fancy value, how do we feed that fancy value into the function?
To best illustrate what we can do with the monad, here we will use the Maybe type .
ghci> (\x -> Just (x+1)) 1 Just 2 ghci> (\x -> Just (x+1)) 100 Just 101
so we will make a function out of it.
applyMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b applyMaybe Nothing f = Nothing applyMaybe (Just x) f = f x
we can do more test.
ghci> Just 3 `applyMaybe` \x -> Just (x+1) Just 4 ghci> Just "smile" `applyMaybe` \x -> Just (x ++ " :)") Just "smile :)"
The formal introduction to the Monads.
the formal definition of the Monad is like this:
class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b x >> y = x >>= \_ -> y fail :: String -> m a fail msg = error msg
though we said that Monad is a special beefed up Applicative, why we don't say that in the header, such like this?
class (Applicative m) = > Monad m where
there is some historical reason here, but it won't affect the fact.
Return function is just the same as the pure function in the applicative functors. what is special is the fail method, and it uses the error function, which basically will trigger an Error exception. it's used by Haskell to enable failure in a special syntactic construct for monads that we'll meet later.
>>= as we saw before, like a function application, it takes a monadic value (instead of a normal valud) and feed it with a function which take a normal value but return a monadic value.
>> is a built-in impl using >>=, it will ignore the first parameter and kept the second one? we will come back to it later.
so as always, let see how the May fit into the Monad.
instance Monad Maybe where return x = Just x Nothing >>= f = Nothing Just x >>= f = f x fail _ = Nothing
so , with this, what we can do with the Maybe using the Monad operator is like this:
ghci> return "WHAT" :: Maybe String Just "WHAT" ghci> Just 9 >>= \x -> return (x*10) Just 90 ghci> Nothing >>= \x -> return (x*10) Nothing
Let's see a real case use scenario with Monad..
see if pierre walking the line with a pole and birds landing on two sides, if the number of birds are less or equal than 2, then pierre is able to master his balance, otherwise, he risks falling off.
here is the type definition that we have.
type Birds = Int type Pole = (Birds,Birds)
we will wrote two functions to simulating the landing operation.
landLeft :: Birds -> Pole -> Pole landLeft n (left,right) = (left + n,right) landRight :: Birds -> Pole -> Pole landRight n (left,right) = (left,right + n)
we can chain the landLeft and landRight operation, just as below.
ghci> landLeft 2 (landRight 1 (landLeft 1 (0,0))) (3,1)
to make it easy to write, we will define an infix operator, just as below...
x -: f = f x
now, we can write as this:
ghci> (0,0) -: landLeft 1 -: landRight 1 -: landLeft 2 (3,1)
however, this does not reflect our code, because it does not meet the requirement if the pole is unbalanced, then poor pierre might fall... like this
ghci> (0,0) -: landLeft 1 -: landRight 4 -: landLeft (-1) -: landRight (-2) (0,2)
or this
ghci> landLeft 10 (0,3) (10,3)
so if we write as landLeft or landRight as
landLeft :: Birds -> Pole -> Maybe Pole landLeft n (left,right) | abs ((left + n) - right) < 4 = Just (left + n, right) | otherwise = Nothing landRight :: Birds -> Pole -> Maybe Pole landRight n (left,right) | abs (left - (right + n)) < 4 = Just (left, right + n) | otherwise = Nothing
but we might not be able to chain the operatoins, because now we might get a Maybe pole and that we stop be able to apply function in the composition fashion.
so you cannot just do
ghci> landLeft 2 (0,0) Just (2,0) ghci> landLeft 10 (0,3) Nothing
and then you cannnot just chain the calles like
(0,0) -: landLeft 1 -: landRight 4 -: landLeft (-1) -: landRight (-2)
because that the return type is Maybe Pole instead of just Pole..
but here the monad come sto rescue, as it take a facny value and a fancy value that a function which takes a normal value and return a fancy value and return a nother fancy value (could be a different type in the box).
so you can chain the call like this ,(because how the monad is used, the landLeft is put in the middle for the chainning) .
ghci> return (0,0) >>= landRight 2 >>= landLeft 2 >>= landRight 2 Just (2,4)
Compare to what it is like this:
ghci> (0,0) -: landLeft 1 -: landRight 4 -: landLeft (-1) -: landRight (-2) (0,2)
but witht he new monaid, we can the real situation covered.
ghci> return (0,0) >>= landLeft 1 >>= landRight 4 >>= landLeft (-1) >>= landRight (-2) Nothing
supose now there is a new method called banana which cause the poor pierre to fall immediately, so here is how it will be implemented just as how..
banana :: Pole -> Maybe Pole banana _ = Nothing
now,you can chain as follow.
ghci> return (0,0) >>= landLeft 1 >>= banana >>= landRight 1 Nothing
and then you can use (>>) which will ignore the input and just return a predetermined value, (this is the meaning of >> means)
(>>) :: (Monad m) => m a -> m b -> m b m >> n = m >>= \_ -> n
now, you do this::
ghci> return (0,0) >>= landLeft 1 >> Nothing >>= landRight 1 Nothing
so you can see Nothing will cause the whole expression to return just nothing.
suppose there is a routine that simulate the landing of birds,
routine :: Maybe Pole routine = case landLeft 1 (0,0) of Nothing -> Nothing Just pole1 -> case landRight 4 pole1 of Nothing -> Nothing Just pole2 -> case landLeft 2 pole2 of Nothing -> Nothing Just pole3 -> landLeft 1 pole3
and this is a verbose version of what we have achived with the >>= and the>> mehtod, the >>= gives us a way to deal with the error condition (failure condition)..
相关推荐
Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...
在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...
Haskell Monads小书 :warning: 初稿! –请仔细阅读 1. do块 2.解释嵌套的Lambda和绑定 =<<作为序列运算符 2C 。 a- a -> M b背后的直觉 2D合成do块 2E 。 嵌套do块 3.实现Monad和封装 3A 。 新型和副...
在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...
**哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...
Get Programming with HASKELL-2018-英文版
Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...
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-...
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 ...
【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...
Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...
在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...
Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...
你可以在找到 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开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...
### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...
### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。