3
votes

I have a predicate in F# for example

let myFunc x y = x < y

Is there a way to create the negated version of this function?

So something that would be functionally similar to

let otherFunc x y = x >= y

But by using the original myFunc?

let otherFunc = !myFunc  // not valid 
2

2 Answers

10
votes

What you are trying to do is called "function composition". Check out the function composition operator of f#:

I don't have a compiler available to experiment, but you could start from

let otherFunc = myFunc >> not

and work your way through errors.

EDIT: Max Malook points out that this will not work with the current definition of myFunc, because it takes two arguments (in a sense, this is functional-land). So, in order to make this work, myFunc would need to change into accepting a Tuple:

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

Negation in F# is done with the function not. The ! operator is for dereferencing ref cells.

let otherFunc x y = not (myFunc x y)