gpt4 book ai didi

f# - 在 F# 中创建否定谓词

转载 作者:行者123 更新时间:2023-12-01 20:25:44 25 4
gpt4 key购买 nike

例如,我在 F# 中有一个谓词

let myFunc x y = x < y

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

所以在功能上类似于
let otherFunc x y = x >= y

但是通过使用原始的 myFunc?
let otherFunc = !myFunc  // not valid 

最佳答案

您正在尝试做的称为“函数组合”。查看f#的函数组合运算符:

  • In F# what does the >> operator mean?

  • 我没有可用于实验的编译器,但您可以从
    let otherFunc = myFunc >> not

    并以自己的方式解决错误。

    编辑 : Max Malook 指出这不适用于 myFunc 的当前定义。 ,因为它需要两个参数(从某种意义上说,这是功能区)。所以,为了使这项工作, myFunc需要更改为接受元组:
    let myFunc (a, b) = a > b
    let otherFunc = myFunc >> not
    let what = otherFunc (3, 4)

    关于f# - 在 F# 中创建否定谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28716957/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com