Haskell : (maybe) monad construction

I'm currently struggling with a new element of Haskell: Monads. Therefore I was introduced to this by an example of creating a >>= operator that executes a function on a maybe type (taking its actual integer value as argument to it) only if it's not equal to Nothing, and otherwise return Nothing: (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b Nothing >>= _ = No

Haskell:(也许)monad构建

我目前正在用Haskell的一个新元素Monads来挣扎。 因此,我通过创建一个>> =运算符的例子来介绍这一点,该运算符在可能类型上执行一个函数(仅将其实际整数值作为它的参数),只要它不等于Nothing,否则返回Nothing: (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b Nothing >>= _ = Nothing (Just x) >>= f = f x 但是,我不太清楚这是如何使用它的以下用法: eval (Val n) = Just n eval

Difference between Monad and Applicative in Haskell

I just read the following from typeclassopedia about the difference between Monad and Applicative . I can understand that there is no join in Applicative . But the following description looks vague to me and I couldn't figure out what exactly is meant by "the result" of a monadic computation/action. So, if I put a value into Maybe , which makes a monad, what is the result of this

Haskell中Monad和Applicative的区别

我刚刚从typeclassopedia中了解了Monad和Applicative之间的区别。 我可以理解, Applicative没有join 。 但是下面的描述对我来说看起来含糊不清,我无法弄清楚单子计算/行为的“结果”究竟意味着什么。 所以,如果我将一个值写入Maybe ,这会产生一个monad,那么这个“计算”的结果是什么? 让我们仔细看看(>> =)的类型。 基本的直觉是它将两个计算组合成一个更大的计算。 第一个参数ma是第一个计算。 然而,如果第

A monad with a "real" return function

A common beginner's mistake is to see return and think that it's a language keyword that exits the current function with a return value. Of course, we know that isn't what it does at all. But I wonder... could we actually make such a function? Purely for argument's sake, at this point. It seems we're looking for some monad Foo which possesses a function exit :: x -> F

具有“真实”返回功能的monad

一个常见的初学者的错误是看到return并认为它是一个语言关键字,它以返回值退出当前函数。 当然,我们知道这不是它所做的。 但我想知道......我们能否真的做出这样的功能? 纯粹为了论证的缘故,在这一点上。 看来我们正在寻找一些具有功能的monad Foo exit :: x -> Foo x 这将中止其余的计算并立即返回x 。 这样的事情是可以修复的吗? 如果可以构建,它会有用吗? 这是一种理智的尝试吗? 是的,在Cont单元中

Haskell monad: etymology versus meaning?

When I'm developing an understanding of some concept, I find it very unsatisfactory not to be able to see how the apparent etymology of the concept name relates to what I think I'm understanding about the concept. If I can't see the connection, I'm left with the feeling that there's some significant insight the name is trying to convey that I haven't yet discovered. Mon

哈斯克尔monad:词源与意义?

当我对某些概念有所了解时,我发现不能理解概念名称的明显词源与我认为对概念的理解有何关系。 如果我看不到这种联系,我会留下这样的感觉,即有一些重要的洞察力是这个名字试图传达的,我还没有发现。 Monad:从希腊统一。 Mon = 1; ad =包含某个数字的组或单位。 这构成了“由一件事组成的一个小组或单位”。 http://www.haskell.org/haskellwiki/All_About_Monads说: “monad是一种用计算值和计算顺序来组织计算的方

why can't a function take monadic value and return another monadic value?

Let's say that we have two monadic functions: f :: a -> m b g :: b -> m c h :: a -> m c The bind function is defined as (>>=) :: m a -> (a -> m b) -> m b My question is why can not we do something like below. Declare a function which would take a monadic value and returns another monadic value? f :: a -> m b g :: m b -> m c h :: a -> m c The

为什么一个函数不能使用monadic值并返回另一个monadic值?

假设我们有两个monadic函数: f :: a -> m b g :: b -> m c h :: a -> m c 绑定函数被定义为 (>>=) :: m a -> (a -> m b) -> m b 我的问题是为什么我们不能做下面的事情。 声明一个函数,它将采用一个monadic值并返回另一个monadic值? f :: a -> m b g :: m b -> m c h :: a -> m c 绑定函数被定义为 (>>=) :: m a -> (ma -> m b) -> m b haskell中限制

Combining monads in Haskell

I am trying to write a Spider Solitaire player as a Haskell learning exercise. My main function will call a playGame function once for each game (using mapM ), passing in the game number and a random generator ( StdGen ). The playGame function should return a Control.Monad.State monad and an IO monad that contains a String showing the game tableau and a Bool indicating if the game was won or l

在Haskell中合并monad

我正在尝试编写一个蜘蛛纸牌游戏作为Haskell学习练习。 我的main功能将为每个游戏调用一次playGame函数(使用mapM ),传入游戏编号和随机生成器( StdGen )。 playGame函数应返回一个Control.Monad.State monad和一个IO monad,其中包含一个显示游戏画面的String和一个表示游戏是赢或输的Bool 。 我如何将State monad与IO monad结合起来得到返回值? `playGame的类型声明应该是什么? playGame :: Int -> StdGen a -&

Haskell Monad Functions

I'm going through a Haskell tutorial and am given this piece of code to do with moving a knight in chess: import Control.Monad type KnightPos = (Int,Int) moveKnight :: KnightPos -> [KnightPos] moveKnight (c,r) = do (c',r') <- [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1) ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2) ] guard (c' `elem` [1..8] &

Haskell Monad函数

我正在浏览一个Haskell教程,并且给出了一段代码,用于在国际象棋中移动骑士: import Control.Monad type KnightPos = (Int,Int) moveKnight :: KnightPos -> [KnightPos] moveKnight (c,r) = do (c',r') <- [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1) ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2) ] guard (c' `elem` [1..8] && r' `elem` [1..8]) return

Haskell Monad bind operator confusion

Okay, so I am not a Haskell programmer, but I am absolutely intrigued by a lot of the ideas behind Haskell and am looking into learning it. But I'm stuck at square one: I can't seem to wrap my head around Monads, which seem to be fairly fundamental. I know there are a million questions on SO asking to explain Monads, so I'm going to be a little more specific about what's bugging

Haskell Monad绑定运营商混淆

好的,所以我不是Haskell程序员,但是我对Haskell背后的很多想法深感兴趣,并且正在研究它。 但是我陷入了一个尴尬的境地:我似乎无法将我的头围绕Monads,这似乎是相当重要的。 我知道有这么多问题要求解释Monads,所以我会更具体地说明有什么问题: 我阅读了这篇优秀的文章(Javascript中的介绍),并认为我完全理解了Monads。 然后我阅读Monads上的维基百科条目,并看到: 一个多态类型(M t)→(t→M u)→(M u)的绑

HaltingProblem in Agda?

I am working through a paper trying to implement their Haskell code in Agda. They want to formulate the halting problem by saying let bot be a program such that for any data type a : bot :: a bot = bot They go on to define data S = T so the Halting problem is said as: The function diverges : S → S defined by diverges(T)= bot diverges(bot)= T is not computable and hence is not definable i

在Agda停止问题?

我正在通过一篇论文试图在Agda中实现他们的Haskell代码。 他们想要制定停机问题,让let bot成为一个程序,以便对于任何数据类型a : bot :: a bot = bot 他们继续定义 data S = T 所以停机问题是这样说的: 功能diverges : S → S定义 diverges(T)= bot diverges(bot)= T 是不可计算的,因此在我们的语言中是不可确定的 我试图在Agda中实现这一点: data S : Set where ⊤ : S ⊥ : _ ⊥ = ⊥ diverges : S → S diverg

Is there a language with constrainable types?

Is there a typed programming language where I can constrain types like the following two examples? A Probability is a floating point number with minimum value 0.0 and maximum value 1.0. type Probability subtype of float where max_value = 0.0 min_value = 1.0 A Discrete Probability Distribution is a map, where: the keys should all be the same type, the values are all Probabilities, and

有限制类型的语言吗?

有没有一种类型化的编程语言,我可以像下面的两个例子那样约束类型? 概率是一个浮点数,最小值为0.0,最大值为1.0。 type Probability subtype of float where max_value = 0.0 min_value = 1.0 离散概率分布是一个映射,其中:键应该都是相同的类型,值都是概率,并且值的总和= 1.0。 type DPD<K> subtype of map<K, Probability> where sum(values) = 1.0 据我所知,Haskell或Agda不可能这样做