Jake Choi
Jake Choi

Reputation: 151

OCaml syntax: variables side-by-side

I feel like I'm missing some important fundamentals regarding this weird language.

Consider the following program:

let q f x = f x x;;
let s = q (+);;

If I run this in OCaml, I get:

val q : ('a -> 'a -> 'b) -> 'a -> 'b = <fun>
val s : int -> int = <fun>

Compared to other OCaml code I've seen, the syntax for this is really weird to me. In the first line, what does setting q to f x x do? What does q (+) do as well (hoping I can understand this if I get the first part)? Any help understanding this code would help.

Upvotes: 0

Views: 48

Answers (1)

Th&#233;o Winterhalter
Th&#233;o Winterhalter

Reputation: 5108

You are probably missing the fact that a space is application of the function.

With parentheses for arguments what you have is

q(f,x)    = f(x,x)
s(n)      = q(plus, n)
plus(k,l) = k + l

(+) being a shortcut for the function which takes x and y and returns x + y, which you can write (+) x x as well.

In your case q (+) is the function

fun x -> x + x

Upvotes: 1

Related Questions