[英]Custom computation expressions in F#
I've been toying with monads in F# (aka computation expressions) and I wrote this simple Identity monad: 我一直在玩F#(也就是计算表达式)中的monads,我写了这个简单的Identity monad:
type Identity<'a> =
| Identity of 'a
type IdentityBuilder() =
member x.Bind (Identity v) f = f(v)
member x.Return v = Identity v
let identity = new IdentityBuilder()
let getInt() = identity { return Int32.Parse(Console.ReadLine()) }
let calcs() = identity {
let! a = getInt() // <- I get an error here
let! b = getInt()
return a + b }
I don't understand the error I'm getting in the marked line: 我不明白我在标记的行中得到的错误:
This expression was expected to have type Identity<'a> but here has type 'b * 'c
该表达式应该具有Identity <'a>类型,但这里的类型为'b *'c
I think this makes no sense as getInt() is clearly a value of type Identity<'a>
. 我认为这没有意义,因为getInt()显然是
Identity<'a>
类型的值。
Can anyone tell me what am I doing wrong? 谁能告诉我我做错了什么?
The computation expression syntax wants Bind
to have a tupled, not curried argument. 计算表达式语法希望
Bind
有一个tupled,而不是curried参数。 So 所以
member x.Bind((Identity v), f) = f(v)
See this article for all signatures. 有关所有签名,请参阅此文章 。
The problem is the type of your Bind
function - it shouldn't take curried arguments. 问题是你的
Bind
函数的类型 - 它不应该采用curried参数。 If you change it to: 如果您将其更改为:
member x.Bind (Identity v, f) = f(v)
then it should work. 那它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.