F#如果没有条件

Fra*_*ale 25 f#

如果写入if条件,F#中最好的方法是什么?

现在我写的是这样的:

if condition <> true then do
Run Code Online (Sandbox Code Playgroud)

有没有其他更短的方式来写它?喜欢用!运营商?

Ale*_*lex 27

如果你认为not也是一个函数,那么你可以将条件输入它中以避免括号如下:

if not <| condition param1 param2 then ...
Run Code Online (Sandbox Code Playgroud)

原因是如果你的条件函数接受参数,你不需要这样做

not (condition param1 param2) 
Run Code Online (Sandbox Code Playgroud)

第一种方式可能有点干净,因为看起来f#支持管道而不是括号用于运算符优先级.

  • “括号太OO了”Lisp想和你说句话;) (3认同)

sma*_*uck 20

在Ocaml中,您可以使用"not"关键字:

if not condition then ...
Run Code Online (Sandbox Code Playgroud)

希望与F#一起工作.

  • 我用<| 运算符没有,像这样:"if not <| expression then ..."以避免使用括号. (5认同)
  • +1:是的,在F#中工作.根据需要使用parens环绕条件. (3认同)

Orb*_*ing 6

not函数,但它只适用于布尔变量.

所以你可以说:

if (not condition) then do
Run Code Online (Sandbox Code Playgroud)

但这不适用于其他类型,如C风格的语言.

不要!意外使用,因为它仍然是F#中的运算符,它是对可变参考单元取消引用.

请参阅完整的操作员文档.