I am trying to define a function wrapper that curries a tuple in SML.
fun curry f = fn (x, y) z => f x y z;
Gives me the error
Non-identifier applied to a pattern.
I am new to ML and not sure why the pattern matching in fn doesn't work.
How could I make this work?
Closures in SML don't allow for multiple arguments, but you can nest them instead.
What
curryusually does is take a functionfthat normally accepts a tuple(x, y)and instead returns a modified function that takesxandyseparately. Here is a number of equivalent ways to definecurry:Its opposite,
uncurryinstead takes a functionfthat takesxandyseparately and returns a modified function that takes(x, y). Here is one way to writeuncurry:It's easy to mix up the two.
One way to fix the function you've written so that it compiles is insert an extra
=> fn:Before giving it a name, let's analyse what it does. It has the type signature:
meaning that it takes a function of three curried arguments (
x,yandz) and returns a modified function where the first two arguments are now in a tuple (uncurried) and the third is still curried. This is really a less generic version ofuncurry. A more clear way of writing it would be:If you use
uncurryon a function of three arguments, you get the same effect, but you can't usewhat_is_thison anything with two curried arguments. So I'd say that this is a less useful variant ofuncurry.There are however other more useful variants of
curry/uncurry. For example, you could make anuncurry_twicethat convertsf x y zinto(uncurry_twice f) ((x, y), z), or anuncurry3that convertsf x y zinto(uncurry3 f) (x, y, z):