Search code examples
functional-programmingocamlfunction-composition

Composite functions in ocaml


How can I define a composite function in a functional language, in particular with Ocaml? For example, if I write a function that calculates the negation of the result of another function, that is: not(f(x)) where f(x) returns a boolean. How can I define it?


Solution

  • Given some function f, that has the type:

    f: 'a -> bool
    

    you want to be able to generate another function to wrap it to negate the result. Let's consider the type for this new function, let's call it negated (I'm not using not since it is the name of a builtin):

    negated: ('a -> bool) -> 'a -> bool
    

    Why is this the type? Why not 'a -> bool? Well remember, we want this new function to take in an existing function and return a new function with the same type that does something different. To see it clearer, you can think of it like this: ('a -> bool) -> ('a -> bool) which is equivalent.

    So now given these constraints, how can we write the negated function?

    let negated f = ??
    

    Well we first have to consider that this function needs to return a function:

    let negated f = (fun x -> ??)
    

    What next? Well, we know that the new function we create should call our wrapped function with the argument and negate it. Let's do that, call the function with the argument: f x, and negate it: not (f x). That gives us the final function definition:

    let negated f = (fun x -> not (f x))
    

    Let's see it in action:

    # let f x = x < 5;;
    val f : int -> bool = <fun>
    # f 2;;
    - : bool = true
    # f 8;;
    - : bool = false
    # let negated f = (fun x -> not (f x));;
    val negated : ('a -> bool) -> 'a -> bool = <fun>
    # let g = negated(f);;
    val g : int -> bool = <fun>
    # g 2;;
    - : bool = false
    # g 8;;
    - : bool = true