Recall that an F# function that takes two arguments can be coded in either uncur
ID: 3585225 • Letter: R
Question
Recall that an F# function that takes two arguments can be coded in either uncurried form (in which case it takes a pair as its input) or curried form (in which case it takes the first argument and returns a function that takes the second argument). In fact it is easy to convert from one form to the other in F#. To this end, define an F# function curry f that converts an uncurried function to a curried function, and an F# function uncurry f that does the opposite conversion. For example,
> (+);;
val it : (int -> int -> int) = <fun:it@13-7>
> let plus = uncurry (+);;
val plus : (int * int -> int)
> plus (2,3);;
val it : int = 5
> let cplus = curry plus;;
val cplus : (int -> int -> int)
> let plus3 = cplus 3;;
val plus3 : (int -> int)
> plus3 10;;
val it : int = 13
What are the types of curry and uncurry?
Explanation / Answer
Curry function example:
1) let curry a = (fun x -> fun y -> a(x,y))
2 )let curry f x y = f (x,y)
val curry : a:('a * 'b -> 'c) -> x:'a -> y:'b -> 'c
In the above example the curry function takes in a function and users another function as input which is evaluated as curry function type.
Both functions return the same type.
Uncurry function example:
1) let uncurry a = (fun (x,y) -> a x y)
2 )let uncurry f (x,y) = f x y
val uncurry : a:('a -> 'b -> 'c) -> x:'a * y:'b -> 'c
In the above example it is uncurry function where a single function is used as an input.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.