在F#中创建否定谓词

时间:2015-02-25 10:46:15

标签: f#

我在F#中有一个谓词

let myFunc x y = x < y

有没有办法创建这个函数的否定版本?

这与功能类似于

的东西
let otherFunc x y = x >= y

但是使用原始的myFunc?

let otherFunc = !myFunc  // not valid 

2 个答案:

答案 0 :(得分:9)

您要做的事情被称为&#34;功能组合&#34;。查看f#的函数组合运算符:

我没有可用于实验的编译器,但您可以从

开始
let otherFunc = myFunc >> not

并逐步解决错误。

编辑:Max Malook指出这不适用于myFunc的当前定义,因为它需要两个参数(从某种意义上说,这是功能土地)。所以,为了使这项工作,myFunc需要转变为接受一个元组:

let myFunc (a, b) = a > b
let otherFunc = myFunc >> not
let what = otherFunc (3, 4)

答案 1 :(得分:3)

F#中的否定是使用函数not完成的。 !运算符用于解除引用ref个单元格。

let otherFunc x y = not (myFunc x y)
相关问题