Possible Duplicate: What is a monad? I am learning to program in the functional language of Haskell and I came across Monads when studying parsers. I had never heard of them before and so I did some extra studying to find out what they are. Everywhere I look in order to learn this topic just confuses me. I can't really find a simple definition of what a Monad is and how to use them.
可能重复: 什么是monad? 我正在学习使用Haskell的函数式语言进行编程,并且在学习解析器时遇到了Monads 。 我以前从来没有听说过他们,所以我做了一些额外的研究,以了解他们是什么。 我为了学习这个主题而到处看到的只是让我感到困惑。 我无法真正找到Monad是什么以及如何使用它们的简单定义。 “monad是一种使用这些值根据值和计算序列构造计算结构的方法” - eh ??? 有人可以提供一个Monad在Haskell中的简单定义
In the chain documentation you find: Calling chain on a wrapped object will cause all future method calls to return wrapped objects as well. When you've finished the computation, use value to retrieve the final value. So does the chain function create a monad? No, not a monad, but a comonad! It turns a function that takes a wrapped object and returns a normal value into a function tha
在chain文档中找到: 在包装对象上调用chain将导致所有将来的方法调用返回包装对象。 完成计算后,使用value来检索最终值。 那么chain函数是否创建了monad? 不,不是一个单子,而是一个共同体! 它变成一个函数,它接受一个包装对象并将一个普通值返回到一个函数中,该函数将获取并返回一个包装对象。 作为Haskell类型的签名将是: (Wrapped a -> b) -> (Wrapped a -> Wrapped b) value的类型签名是: Wrap
Many of us don't have a background on functional programming, and much less on category theory algebra. So let's suppose that we need and therefore create a generic type like data MySomething t = ....... Then we continue programming, and using MySomething . What evidences should alert us that MySomething is a monad, and that we have to make it one by writing instance Monad MySomethin
我们中的许多人没有功能编程的背景,更不用说类别理论代数。 所以我们假设我们需要并因此创建一个通用类型 data MySomething t = ....... 然后我们继续编程,并使用MySomething 。 有什么证据应该提醒我们MySomething是一个monad,我们必须通过编写instance Monad MySomething ...来实现它,并且定义return和>>=它是什么? 谢谢。 编辑:另请参阅此问题:链接操作是monad类解决的唯一事情吗?,而这个答案monad是
Most of the monad explanations use examples where the monad wraps a value. Eg Maybe a , where the a type variable is what's wrapped. But I'm wondering about monads that never wrap anything. For a contrived example, suppose I have a real-world robot that can be controlled, but has no sensors. Maybe I'd like to control it like this: robotMovementScript :: RobotMonad () robotMoveme
大部分monad解释都使用monad包装值的例子。 例如, Maybe a ,其中a类型变量是包装的。 但我想知道monads不会包装任何东西。 对于一个人为的例子,假设我有一个可以控制的现实世界的机器人,但没有传感器。 也许我想要这样控制它: robotMovementScript :: RobotMonad () robotMovementScript = do moveLeft 10 moveForward 25 rotate 180 main :: IO () main = liftIO $ runRobot robotMovementScript connectTo
Monads are usually explained in turns of return and bind . However, I gather you can also implement bind in terms of join (and fmap ?) In programming languages lacking first-class functions, bind is excruciatingly awkward to use. join , on the other hand, looks quite easy. I'm not completely sure I understand how join works, however. Obviously, it has the [Haskell] type join :: Monad
Monads通常会依次解释return和bind 。 但是,我收集你也可以在join (和fmap ?)方面实现bind 在缺乏一流功能的编程语言中, bind使用起来非常尴尬。 另一方面, join看起来相当容易。 然而,我并不完全确定我明白join如何运作。 显然,它有[Haskell]类型 join :: Monad m => m (m x) -> m x 对于monad列表,这是简单而明显的concat 。 但对于一般的monad来说,这种方法实际上在做什么呢? 我看到它对类型签名有什
Having briefly looked at Haskell recently, what would be a brief, succinct, practical explanation as to what a monad essentially is? I have found most explanations I've come across to be fairly inaccessible and lacking in practical detail. First: The term monad is a bit vacuous if you are not a mathematician. An alternative term is computation builder which is a bit more descriptive of w
最近简单地看了一下Haskell,对于monad实质上是什么而言,简单,简洁,实用的解释是什么? 我发现我所遇到的大部分解释都是相当难以获得的,并且缺乏实际的细节。 第一:如果你不是数学家, monad这个词有点空虚。 另一个术语是计算生成器 ,它对它们实际上有用的内容有更多描述。 你问一些实际的例子: 示例1:列表理解 : [x*2 | x<-[1..10], odd x] 该表达式返回1到10范围内所有奇数的双精度。非常有用! 事
I have a first typeclass which accepts lists of lists of lists of … of leaf : {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-} class ListTree leaf t where lmap :: (leaf -> leaf) -> t -> t instance ListTree leaf leaf where lmap f v = f v instance ListTree leaf t => ListTree leaf [t] where lmap f v = map (lmap f) v I have a second typeclass which accep
我有接受的名单...的名单列表第一类型类leaf : {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-} class ListTree leaf t where lmap :: (leaf -> leaf) -> t -> t instance ListTree leaf leaf where lmap f v = f v instance ListTree leaf t => ListTree leaf [t] where lmap f v = map (lmap f) v 我有接受2元组和三元组的第二类型类a : class Tups a t where tmap ::
the title of this is admittedly not very descriptive, but I don't know how else to describe this in a short title. I'd appreciate any recommendations! I'm going to be presenting a very simplified version of my issue :) So I have a typeclass class Known f a where known :: f a That is supposed to be able to generate the canonical construction of a given type at a certain inde
这个标题确实不是很具描述性,但我不知道如何用简短的标题来描述这个。 我会很感激任何建议! 我将介绍我的问题的一个非常简化的版本:) 所以我有一个typeclass class Known f a where known :: f a 这应该能够在特定的索引处生成给定类型的规范构造,对于使用GADT和其他东西非常有用。 我给出了这个简化版本,以及相关的部分。 所以显然有一个Proxy实例: instance Known Proxy a where known = Proxy 我们
Consider such domain logic: three types of users: Civilians, ServiceMembers and Veterans. Each of them has 'name', stored in different attributes. Task is to write a function, accepting each of the types and returning 'C' char for Civilians, 'V' char for Veterans and 'S' char for ServiceMembers. I have such record declarations: data ServiceMemberInfo = Servic
考虑这样的领域逻辑:三种类型的用户:平民,服务成员和退伍军人。 每个人都有'名字',存储在不同的属性中。 任务是编写一个函数,接受每种类型,并返回平民的'C'字符,退伍军人的'V'字符和ServiceMembers的'S'字符。 我有这样的记录声明: data ServiceMemberInfo = ServiceMemberInfo { smname::String } data VeteranInfo = VeteranInfo { vname::String } data CivilianInfo = Civilia
I want to be able to have a function, which implementation will choose a typeclass based on the manual type specification of it's return type. Here's a contrived example: a typeclass and two instances: class ToString a where toString :: a -> String instance ToString Double where toString = const "double" instance ToString Int where toString = const "int" I'm able to cho
我希望能够有一个函数,该实现将基于它的返回类型的手动类型规范来选择一个类型类型。 这是一个人为的例子:一个typeclass和两个实例: class ToString a where toString :: a -> String instance ToString Double where toString = const "double" instance ToString Int where toString = const "int" 我可以通过用Int类型调用toString来选择实例: function :: String function = toString (undefined :: Int)