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:

  1. let abs_diff = fun x -> fun y -> abs(x-y) returns function 1 parameter fun y -> abs (x-y) , inside it, x parameter

  2. so, when apply abs_diff 5 6, firsts takes first argument 5, returns function fun y -> abs (5-y), continue apply argument 6, 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

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -