Jackson Tale
Jackson Tale

Reputation: 25842

Is this OCaml function composition operator definition/expression correct?

let (++) f g x = f (g x) in
    let f x = x + 1 in
    let g x = x * 2 in
    (f++g) 1;;
  1. Is the above expression correct?
  2. It seems to me that the above code should be just like defining f++g x = 2 * x + 1. Am I correct?

Upvotes: 4

Views: 452

Answers (1)

codablank1
codablank1

Reputation: 6195

Your implementation of function composition is correct, since :

(g ∘ f)(x) = g(f(x)) for all x in X

according to wikipedia

I get :

- : int = 3

in ocamlktop

Upvotes: 4

Related Questions