例如,我在F#中有一个谓词
let myFunc x y = x < y
Run Code Online (Sandbox Code Playgroud)
有没有办法创建此功能的否定版本?
所以在功能上类似的东西
let otherFunc x y = x >= y
Run Code Online (Sandbox Code Playgroud)
但是通过使用原始的myFunc?
let otherFunc = !myFunc // not valid
Run Code Online (Sandbox Code Playgroud)
你要做的是所谓的"功能组合".查看f#的函数组合运算符:
我没有可用于实验的编译器,但您可以从中开始
let otherFunc = myFunc >> not
Run Code Online (Sandbox Code Playgroud)
并通过错误工作.
编辑:Max Malook指出这不适用于当前的定义myFunc
,因为它需要两个参数(在某种意义上,这是功能土地).所以,为了使这项工作,myFunc
需要改为接受一个元组:
let myFunc (a, b) = a > b
let otherFunc = myFunc >> not
let what = otherFunc (3, 4)
Run Code Online (Sandbox Code Playgroud)