The argument taken order of curried functions in OCaml -
normally, write function
let abs_diff x y = abs (x-y)
also can have curried version
:
let abs_diff = fun x -> fun y -> abs(x-y)
i write question here want confirm understand currying correctly.
here understanding:
let abs_diff = fun x -> fun y -> abs(x-y)
returns function 1 parameterfun y -> abs (x-y)
, inside it, x parameterso, when apply
abs_diff 5 6
, firsts takes first argument5
, returns functionfun y -> abs (5-y)
, continue apply argument6
, final result(fun y -> abs (5-y)) 6
am correct?
furthermore, deal function application, ocaml interpreter utop, ocaml doing point 2 above?
first of all, 2 writing
let abs_diff x y = abs (x-y) let abs_diff = fun x -> fun y -> abs(x-y)
are equivalent. both define same function of type
val abs_diff : int -> int -> int = <fun>
an uncurried version have been
# let abs_diff (x,y) = abs(x-y);; val abs_diff : int * int -> int = <fun>
that function taking 1 parameter namely couple (x,y)
of type int*int
.
it not clear me mean
and inside it, x parameter
do mean x
replaced parameter ?
what happening call abs_diff 5
return closure, function context. like
fun y -> abs (x-y) x = 5
which of course equivalent to
fun y -> abs (5-y)
note compiler able optimize function manipulation simple function call if need.
Comments
Post a Comment