Reputation: 117
I have a question why does OCaml behaves somewhat unusual. By defining the function
let abs_diff x y = abs(x - y);;
we get val abs_diff : int -> int -> int = <fun>
now by defining as
let abs_diff x y =
fun x -> (fun y -> abs(x - y));;
val abs_diff : 'a -> 'b -> int -> int -> int = <fun>
now using another function called as
let dist_from3 = abs_diff 3;;
with the first definition it works perfectly but with the second one it does not work as expected. We get that it is
val dist_from3 : '_a -> int -> int -> int = <fun>
why is it behaving like that, and why are those two definition of the at first look same function different?
Upvotes: 1
Views: 52
Reputation: 66818
In your second definition you have two distinct appearances (bindings) of x
and y
. That's why there are four parameters in the result. This is what you want:
let abs_diff = fun x -> fun y -> abs (x - y)
(FWIW in actual practice I sometimes make this mistake, especially when using the function
keyword.)
Upvotes: 5