我有一个F#中的谓词示例
let myFunc x y = x < y
有没有办法创建这个函数的否定版本?
因此,与以下内容在功能上类似的东西:
let otherFunc x y = x >= y
但是通过使用原始的myFunc函数呢?
let otherFunc = !myFunc // not valid
我有一个F#中的谓词示例
let myFunc x y = x < y
let otherFunc x y = x >= y
但是通过使用原始的myFunc函数呢?
let otherFunc = !myFunc // not valid
你正在尝试的是所谓的“函数组合”。查看F#中的函数组合运算符:
我没有可用于实验的编译器,但您可以从以下内容开始。
let otherFunc = myFunc >> not
并且逐步解决错误。
编辑:Max Malook 指出,这将无法使用当前定义的 myFunc
工作,因为它需要两个参数(在某种意义上,这是函数领域)。 因此,为了使其工作, myFunc
需要改为接受一个元组:
let myFunc (a, b) = a > b
let otherFunc = myFunc >> not
let what = otherFunc (3, 4)
F# 中的否定(Negation)使用函数not
。而!
操作符用于取消引用ref
单元格。
let otherFunc x y = not (myFunc x y)
not << myFunc
来困扰你的同事们。 - gypsydave5