3 ms·
Good explanation! Wish I knew Rust (which I do often), now, instead of just Haskell, heheh. For anyone curious what "currying" actually is (I must say that I,
by anyfoo 4y ago
Good explanation! Wish I knew Rust (which I do often), now, instead of just Haskell, heheh.
For anyone curious what "currying" actually is (I must say that I, for quite some time, made the mistake of thinking that taking a function that takes n arguments and getting a function that takes n-1 arguments (with one fixed) is "currying", which is not technically true, it's just a good thing you can do after currying):
Currying is the act of taking a function that takes n arguments (or an n-tuple), and returning a new function with only one argument. But that function effectively wraps another function, and this repeats n times. So, you take e.g. this function:
f(x,y,z)
and currying makes this function:
g(z)
You see it only has one argument. But when you call that function with its only argument, the result is actually another function:
h(y)
There's our second argument. Call that and you get:
i(x)
And now you can call that with the first argument, and get the result.
So after currying a function, you can just apply the last argument, and effectively get back a function with the last argument "fixed". Without that, you effectively have to use a lambda to fix the argument. (Or a combinator that does it for you.)
Though you will need some sort of lambda or combinator anyway, if it's not the last argument you want to fix, but some other one. Then you have to create functions with stuff flipped around... Functions in Haskell usually take care that the last argument is the one where you'd most often want to fix it to a value.