Reputation: 151
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
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