简体   繁体   English

Int选项而不是F#中的Int

[英]Int Option instead of Int in F#

I am having trouble with the following: 我在以下方面遇到麻烦:

let safeDiv x y = 
  match (x,y) with
  | (_, Some 0) -> None
  | (Some xx, Some yy) -> Some (xx/yy)
  | _ -> None

When I go to run this simple function in the interactive window of Visual Studio like so: 当我像这样在Visual Studio的交互式窗口中运行此简单函数时:

safeDiv 4 2

I get the following error... 我收到以下错误...

This expression was expected to have type int option but here has type int. 该表达式应具有类型为int的选项,但此处具有类型为int的选项。

Could it be I'm meant to use safeDiv Some(4) Some(2) ? 难道我是要使用safeDiv Some(4) Some(2)吗? This doesn't work either... 这也不起作用...

Ok, this is overkill but I actually did something similar to this recently. 好的,这太过分了,但是最近我实际上做了类似的事情。

First I defined a computation expression builder for the option type: 首先,我为选项类型定义了一个计算表达式生成器:

type OptionBuilder() =
    member this.Bind(x, f) = Option.bind f x
    member this.Return(x) = Some x
    member this.ReturnFrom(x) = x

let opt = new OptionBuilder()

And then I defined a function sub of type float -> float -> float option 然后我定义了一个float类型的功能子-> float-> float选项

let sub x y = if y = 0.0 then None else Some (x / y)

And finally I used the OptionBuilder to define saveDiv as float option -> float option -> float option 最后我使用OptionBuilder将saveDiv定义为float选项-> float选项-> float选项

let safeDiv x y = opt { let! a = x
                        let! b = y
                        return! sub a b }

You can read more about computation expressions on wikibooks: http://en.wikibooks.org/wiki/F_Sharp_Programming/Computation_Expressions 您可以在Wikibooks上阅读有关计算表达式的更多信息: http : //en.wikibooks.org/wiki/F_Sharp_Programming/Computation_Expressions

And if you want to dive deeper into the theory behind this, you can read this paper by Tomas Petricek and Don Syme: http://www.cl.cam.ac.uk/~tp322/drafts/notations.pdf 如果您想深入了解其背后的理论,可以阅读Tomas Petricek和Don Syme撰写的这篇论文: http ://www.cl.cam.ac.uk/~tp322/drafts/notations.pdf

Your second version was close. 您的第二个版本已经关闭。

It should be 它应该是

safeDiv (Some(4)) (Some(2))

The extra brackets are required to make sure that functions are applied in the correct order. 需要使用额外的括号来确保按正确的顺序应用功能。

You constructed a function that has the signature safeDiv : int option -> int option -> int option . 您构造了一个具有签名safeDiv : int option -> int option -> int option的函数safeDiv : int option -> int option -> int option You need to use an entry like safeDiv (Some 4) (Some 2) to use your function as is. 您需要使用诸如safeDiv (Some 4) (Some 2)类的条目来按safeDiv (Some 4) (Some 2)使用您的功能。

The problem is in the matching of (4, 2), of type int*int, with the expressions (_, Some 0) and (Some xx, Some yy). 问题在于int * int类型的(4,2)与表达式(_,Some 0)和(Some xx,Some yy)的匹配。 The whole function can be simplified: 整个功能可以简化:

let safeDiv x y = 
    match (x,y) with
    | (_, 0) -> None
    | (_, _) -> Some (x/y)

Making the following call valid 使以下通话有效

safeDiv 4 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM