Monad和功能之间的区别 [英] Difference between Monads and Functions

查看:95
本文介绍了Monad和功能之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,关于 Monad ,我知道有人问了很多问题.我不想打扰任何人再问什么是单子.

Ok, about Monad, I am aware that there are enough questions having been asked. I am not trying to bother anyone to ask what is monad again.

实际上,我读了什么是单子?,它非常有帮助.而且我觉得我真的很了解它.

Actually, I read What is a monad?, it is very helpful. And I feel I am very close to really understand it.

我在这里提出这个问题只是为了描述我对Monad和Function的一些想法,并希望有人可以纠正我或确认它正确.

I create this question here is just to describe some of my thoughts on Monad and Function, and hope someone could correct me or confirm it correct.

那篇文章中的一些答案让我觉得 monad 有点像 function .

Some answers in that post let me feel monad is a little bit like function.

Monad接受一个类型,返回一个包装器类型(return),也可以接受一个类型,对其进行一些操作,然后返回一个包装器类型(bind).

Monad takes a type, return a wrapper type (return), also, it can take a type, doing some operations on it and returns a wrapper type (bind).

从我的角度来看,它有点像 function .函数需要执行一些操作并返回某些内容.

From my point of view, it is a little bit like function. A function takes something and do some operations and return something.

然后为什么我们甚至需要monad ?我认为关键原因之一是monad为初始数据/类型上的顺序操作提供了更好的方式或模式.

Then why we even need monad? I think one of the key reasons is that monad provides a better way or pattern for sequential operations on the initial data/type.

例如,我们有一个初始整数i.在我们的代码中,我们需要逐步应用10个函数f1, f2, f3, f4, ..., f10,即,首先在i上应用f1,得到一个结果,然后在该结果上应用f2,然后得到一个新的结果,然后应用f3 ...

For example, we have an initial integer i. In our code, we need to apply 10 functions f1, f2, f3, f4, ..., f10 step by step, i.e., we apply f1 on i first, get a result, and then apply f2 on that result, then we get a new result, then apply f3...

我们可以像f1 i |> f2 |> f3...一样通过原始函数来完成此操作.但是,步骤中的中间结果不一致.同样,如果我们必须在中间某个地方处理可能的失败,事情也会变得很丑陋.如果我们不希望整个过程因异常而失败,则必须构造一个Option.很自然,monad进来了.

We can do this by functions rawly, just like f1 i |> f2 |> f3.... However, the intermediate results during the steps are not consistent; Also if we have to handle possible failure somewhere in middle, things get ugly. And an Option anyway has to be constructed if we don't want the whole process fail on exceptions. So naturally, monad comes in.

Monad在所有步骤中统一并强制返回类型.这在很大程度上简化了代码的逻辑和可读性(这也是design patterns的目的,不是吗).而且,它更能防止错误或错误.例如,Option Monad强制每个中间结果为options,并且很容易实现fast fail范例.

Monad unifies and forces the return types in all steps. This largely simplifies the logic and readability of the code (this is also the purpose of those design patterns, isn't it). Also, it is more bullet proof against error or bug. For example, Option Monad forces every intermediate results to be options and it is very easy to implement the fast fail paradigm.

就像描述过许多有关monad的帖子一样,monad是一种设计模式,也是一种将功能/步骤组合起来以构建流程的更好方法.

Like many posts about monad described, monad is a design pattern and a better way to combine functions / steps to build up a process.

我理解正确吗?

推荐答案

在我看来,您正在通过类比发现学习的极限. Monad被精确地定义为Haskell中的类型类和范畴论中的代数事物.任何使用"...喜欢..."的比较都是不精确的,因此是错误的.

It sounds to me like you're discovering the limits of learning by analogy. Monad is precisely defined both as a type class in Haskell and as a algebraic thing in category theory; any comparison using "... like ..." is going to be imprecise and therefore wrong.

所以不,因为Haskell的monad与函数不同,因为它们是1)实现为类型类,2)打算与函数不同地使用.

So no, since Haskell's monads aren't like functions, since they are 1) implemented as type classes, and 2) intended to be used differently than functions.

这个答案可能不令人满意;您在寻找直觉吗?如果是这样,我建议您做很多例子,尤其是通读 LYAH .如果没有扎实的范例和经验基础,很难对诸如monads之类的抽象事物有一个直观的了解.

This answer is probably unsatisfying; are you looking for intuition? If so, I'd suggest doing lots of examples, and especially reading through LYAH. It's very difficult to get an intuitive understanding of abstract things like monads without having a solid base of examples and experience to fall back on.

为什么我们甚至需要单子?这是一个好问题,也许这里不止一个问题:

Why do we even need monads? This is a good question, and maybe there's more than one question here:

  1. 为什么我们甚至需要Monad类型类?出于同样的原因,我们需要任何类型类.

  1. Why do we even need the Monad type class? For the same reason that we need any type class.

为什么我们甚至需要monad概念?,因为它很有用.另外,它不是函数,因此不能由函数代替. (您的示例似乎不需要不需要Monad(而是需要Appadative)).

Why do we even need the monad concept? Because it's useful. Also, it's not a function, so it can't be replaced by a function. (Your example seems like it does not require a Monad (rather, it needs an Applicative)).

例如,您可以使用Applicative类型类来实现上下文无关的解析器组合器.但是,请尝试在没有Monad的情况下,针对由相同的符号字符串组成的语言两次实施解析器(以空格分隔),即:

For example, you can implement context-free parser combinators using the Applicative type class. But try implementing a parser for the language consisting of the same string of symbols twice (separated by a space) without Monad, i.e.:

a a   -> yes
a b   -> no
ab ab -> yes
ab ba -> no

那是monad提供的一件事:使用以前的结果来决定"做什么的能力.这是另一个示例:

So that's one thing a monad provides: the ability to use previous results to "decide" what to do. Here's another example:

f :: Monad m => m Int -> m [Char]
f m = 
    m >>= \x -> 
    if x > 2 
        then return (replicate x 'a') 
        else return []

f (Just 1)  -->>  Just ""
f (Just 3)  -->>  Just "aaa"
f [1,2,3,4] -->>  ["", "", "aaa", "aaaa"]

这篇关于Monad和功能之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆