F# 在F中创建否定谓词#
例如,我在F#中有一个谓词F# 在F中创建否定谓词#,f#,F#,例如,我在F#中有一个谓词 let myFunc x y = x < y 但是通过使用原始的myFunc let otherFunc = !myFunc // not valid F#中的否定是通过函数not完成的。运算符用于取消引用ref单元格 let otherFunc x y = not (myFunc x y) 你要做的就是所谓的“函数合成”。检查f#的函数复合运算符: 我没有可用的编译器进行实验,但您可以从 let otherFunc = myFunc >&
let myFunc x y = x < y
但是通过使用原始的myFunc
let otherFunc = !myFunc // not valid
F#中的否定是通过函数not
完成的。代码>运算符用于取消引用ref
单元格
let otherFunc x y = not (myFunc x y)
你要做的就是所谓的“函数合成”。检查f#的函数复合运算符:
我没有可用的编译器进行实验,但您可以从
let otherFunc = myFunc >> not
努力克服错误
EDIT:Max Malook指出,这对当前的myFunc
定义不起作用,因为它需要两个参数(从某种意义上说,这是函数land)。因此,为了实现这一点,myFunc
需要更改为接受元组:
let myFunc (a, b) = a > b
let otherFunc = myFunc >> not
let what = otherFunc (3, 4)
只有当您提供第一个参数时,它才会起作用。让otherFunc x=myFunc x>>not@MaxMalook这取决于函数的类型和上下文。如果可以推断出最终的类型(在实际用例中经常是这样),那么组合是有效的。为了简洁起见,我更喜欢函数组合,因为需要注意附加的命名值。为了完整性和可能的娱乐性(如果不清楚的话),也可以编写不
let myFunc (a, b) = a > b
let otherFunc = myFunc >> not
let what = otherFunc (3, 4)