非编程术语中的 Monad [英] Monad in non-programming terms

查看:25
本文介绍了非编程术语中的 Monad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
什么是单子?

您如何用非编程术语来描述 monad?是否有一些编程之外的概念/事物(所有编程之外,而不仅仅是 FP)可以说是在一种重要的方式中起作用或类似于 monad?

How would you describe a monad in non-programming terms? Is there some concept/thing outside of programming (outside of all programming, not just FP) which could be said to act or be monad-like in a significant way?

推荐答案

这是我目前的尝试:

Monads 是桶旅:

  1. 每次操作都是一个人排队;即,操作发生的顺序是明确的.
  2. 每个人将一个桶作为输入,从中取出东西,然后将新东西放入桶中.桶又被传递给旅中的下一个人(通过绑定,或 >>= 操作).
  3. return 操作只是将东西放入桶中的操作.
  4. 在序列 (>>>) 操作的情况下,存储桶的内容在传递给下一个人之前会被转储.下一个人不在乎桶里有什么,他们只是在等待接收.
  5. () 上的 monad 的情况下,一张票正在桶内传递.它被称为单位",它只是一张白纸.
  6. 在 IO monad 的情况下,每个人都会大声说一些非常深刻或非常愚蠢的事情——但他们只有在拿着桶的时候才能说话.
  1. Each operation is a person standing in line; i.e. there's an unambiguous sequence in which the operations take place.
  2. Each person takes one bucket as input, takes stuff out of it, and puts new stuff in the bucket. The bucket, in turn, is passed down to the next person in the brigade (through the bind, or >>=, operation).
  3. The return operation is simply the operation of putting stuff in the bucket.
  4. In the case of sequence (>>) operations, the contents of the bucket are dumped before they're passed to the next person. The next person doesn't care what was in the bucket, they're just waiting to receive it.
  5. In the case of monads on (), a ticket is being passed around inside the bucket. It's called "the Unit", and it's just a blank sheet of paper.
  6. In the case of IO monads, each person says something aloud that's either utterly profound or utterly stupid – but they can only speak when they're holding the bucket.

希望这会有所帮助.:-)

Hope this helps. :-)

感谢您的支持,但遗憾的是,Monad 教程的诅咒再次袭来.我所描述的只是带有容器的函数应用程序,而不是 monad!但我不是虚无主义者——我相信 Monad 教程的诅咒可以被打破!所以这里有一张有点,嗯,复杂的图片,我认为它更好地描述了它.您决定是否值得带给您的朋友.

I appreciate your support, but sadly, the Monad Tutorial curse has struck again. What I've described is just function application with containers, not monads! But I'm no nihilist – I believe the Monad Tutorial curse can be broken! So here's a somewhat more, um, complicated picture that I think describes it a bit better. You decide whether it's worth taking to your friends.

Monads 是一个项目经理团队.除了大队的第一个成员之外,项目经理支持所有人.水桶大队的队员们坐在凳子上,面前放着水桶.

Monads are a bucket brigade with project managers. The project managers stand behind all but the first member of the brigade. The members of the bucket brigade are seated on stools, and have buckets in front of them.

第一个人收到一些东西,用它做一些事情,然后把它放进一个桶里.然后那个人放手——不要交给旅中的下一个人,那太容易了!:-) – 但对于站在那个人后面的项目经理.

The first person receives some stuff, does something with it, and puts it in a bucket. That person then hands off – not to the next person in the brigade, that would be too easy! :-) – but to the project manager standing behind that person.

项目经理(她的名字是 bind>>>=)拿起桶并决定如何处理它.她可能会决定把第一个人的东西从桶里拿出来,然后毫不费力地把它交给她面前的人(这就是 IO monad).她可以选择扔掉水桶并结束旅(那是fail).她可能决定绕过她面前的人,然后将桶直接交给旅中的下一位经理(这就是 Maybe monad 中的 Nothing 发生的情况).她甚至可能决定从桶中取出东西,一次一件递给她面前的人!(这就是 List monad.)在序列 (>>>) 的情况下,她只是轻拍面前人的肩膀,而不是递给他们任何东西.

The project manager (her name is bind, or >>=) takes the bucket and decides what to do with it. She may decide to take the first person's stuff out of the bucket and just hand it to the person in front of her without further ado (that's the IO monad). She may choose to throw the bucket away and end the brigade (that's fail). She may decide to just bypass the person in front of her and pass the bucket to the next manager in the brigade without further ado (that's what happens with Nothing in the Maybe monad). She may even decide to take the stuff out of the bucket and hand it to the person in front of her a piece at a time! (That's the List monad.) In the case of sequence (>>) she just taps the shoulder of the person in front of her, instead of handing them any stuff.

当下一个人制作一桶东西时,该人将其交给下一位项目经理.下一个项目经理再次弄清楚如何处理她给的桶,并将桶中的东西交给她的人.最后,bucket 被back 传递给项目经理链,他们可以选择对bucket 做一些事情(比如组装所有结果的List monad).结果第一个项目经理产生了一桶东西.

When the next person makes a bucket of stuff, the person hands it to the next project manager. The next project manager figures out again what to do with the bucket she's given, and hands the stuff in the bucket to her person. At the end, the bucket is passed back up the chain of project managers, who can optionally do stuff with the bucket (like the List monad assembling all the results). The first project manager produces a bucket of stuff as the result.

do 语法的情况下,每个人实际上都是在之前所有内容的上下文中现场定义的操作 - 好像项目经理传递的不仅仅是内容中的内容桶,还有旅的前任成员产生的价值(呃,东西).如果您使用 bind 和 sequence 而不是使用 do 语法写出计算,则在这种情况下的上下文构建更容易查看——注意每个连续的语句"都是在操作中构造的匿名函数在那之前.

In the case of the do syntax, each person is actually an operation that's defined on the spot within the context of everything that's gone before – as if the project manager passes along not just what's in the bucket, but also the values (er, stuff) that have been generated by the previous members of the brigade. The context building in this case is much easier to see if you write out the computation using bind and sequence instead of using the do syntax – note each successive "statement" is an anonymous function constructed within the operation that's preceded that point.

() 值、IO monad 和 return 操作保持如上描述.

() values, IO monads, and the return operation remain described as above.

但这太复杂了!为什么人们不能自己卸下水桶?"我听到你问.嗯,项目经理可以在幕后做很多工作,否则会使这个人的工作复杂化.我们正在努力让这些旅成员轻松自在,因此他们不必做太多事情.例如,在 Maybe monad 的情况下,每个人都不必检查他们得到的东西的价值,看看他们是否得到了Nothing"——项目经理会替他们处理好.

"But this is too complicated! Why can't the people just unload the buckets themselves?" I hear you ask. Well, the project manager can do a bunch of work behind the scenes that would otherwise complicate the person's work. We're trying to make it easy on these brigade members, so they don't have to do too much. In the case of the Maybe monad, for example, each person doesn't have to check the value of what they're given to see if they were given Nothing – the project manager takes care of that for them.

那么,如果你真的想让每个人的工作更轻松,为什么不一路走下去——让一个人只拿东西和交东西,让项目经理担心分桶?"经常这样做,它有一个特殊的名字,叫做将人(呃,操作)提升到 monad.但是,有时,您希望一个人做一些更复杂的事情,他们希望对生成的存储桶进行一些控制(例如,在 Nothing 的情况下,他们是否需要返回 Nothing> 可能 monad),这就是 monad 提供的全部通用性.

"Well, then, if you're realliy trying to make each person's job easier, why not go all the way – have a person just take stuff and hand off stuff, and let the project manager worry about the bucketing?" That's often done, and it has a special name called lifting the person (er, operation) into the monad. Sometimes, though, you want a person that has something a bit more complicated to do, where they want some control over the bucket that's produced (e.g. whether they need to return Nothing in the case of the Maybe monad), and that's what the monad in full generality provides.

要点是:

  1. 操作是有序的.
  2. 每个人都知道如何制作桶,但不知道如何从桶中取出东西.
  3. 每个项目经理都知道如何处理桶,以及如何从桶中取出东西,但并不关心桶里有什么.

我的睡前教程到此结束.:-P

Thus ends my bedtime tutorial. :-P

这篇关于非编程术语中的 Monad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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