If you look at types, you will clearly see what curry and uncurry .
Remember that you can define a function that either takes its arguments as one big tuple, or as several arguments (in reality, it becomes a βchainβ of functions, each of which takes one argument, see this wiki ):
fun foo (a,x) = a*x+10 fun bar ax = a*x+20
The difference is clearly visible in their types:
val foo = fn : int * int -> int val bar = fn : int -> int -> int
The curry function "converts" a function that takes its arguments as a tuple into a "chain" of functions, each of which takes 1 of the arguments. This is especially convenient when we want to compose a series of functions in which some of them are partially used with arguments. See how the foo type changes:
- curry foo; val it = fn : int -> int -> int
Now we can try to make up two functions:
- (curry foo 5 o bar 1) 4; val it = 130 : int
The first 4 is applied to bar 1 as the argument x , then the result of this calculation ( bar 1 4 ) is given as the argument x for foo .
Obviously, uncurry used for the reverse process:
- uncurry bar; val it = fn : int * int -> int
source share