Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 在F中的选择之上构建(或结果)#_F#_Monads - Fatal编程技术网

F# 在F中的选择之上构建(或结果)#

F# 在F中的选择之上构建(或结果)#,f#,monads,F#,Monads,我根据Scott Wlaschin的信息构建了一个成功/失败的monad,并从中获得了额外的帮助。我最终得到了一个类型 type Result<'a> = | Success of 'a | Error of string 您需要让Error=Choice2Of2。注意大写的O您还需要一个活动模式: type Result<'a> = Choice<'a,string> let Success x :Result<'a> = Choice1O

我根据Scott Wlaschin的信息构建了一个成功/失败的monad,并从中获得了额外的帮助。我最终得到了一个类型

type Result<'a> = 
| Success of 'a
| Error of string

您需要
让Error=Choice2Of2
。注意大写的
O

您还需要一个活动模式:

type Result<'a> = Choice<'a,string>
let  Success x :Result<'a> = Choice1Of2 x
let  Error   x :Result<'a> = Choice2Of2 x
let  (|Success|Error|) = function Choice1Of2 x -> Success x | Choice2Of2 x -> Error x

我就是这样做的。

谢谢。我真是太蠢了。关于使用错误作为模式鉴别器,我仍然会遇到一个错误,所以我会修正这个问题。谢谢。那是丢失的一块。
    match getMetaPropertyValue doc name with
    | Error msg -> ()
    | Success value -> value
type Result<'a> = Choice<'a,string>
let  Success x :Result<'a> = Choice1Of2 x
let  Error   x :Result<'a> = Choice2Of2 x
let  (|Success|Error|) = function Choice1Of2 x -> Success x | Choice2Of2 x -> Error x
type Either<'a,'b> = Choice<'b,'a>
let  Right x :Either<'a,'b> = Choice1Of2 x
let  Left  x :Either<'a,'b> = Choice2Of2 x
let  (|Right|Left|) = function Choice1Of2 x -> Right x | Choice2Of2 x -> Left x