What are curry and high order irregular functions in ML

fun curry fxy = f (x, y); fun uncurry f (x, y) = fxy; fun compose (f, g) x = f (gx); 

I understand that I will compose a function, but I do not quite understand curry and indecision in ML. Can anyone explain this?

Also, what do the following two lines mean?

 (1) compose (compose, uncurry compose) (2) compose (uncurry compose, compose) 
+4
source share
1 answer

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 
+9
source

Source: https://habr.com/ru/post/1384835/


All Articles