Functional Programming- Currying
February 7, 2013 Leave a comment
Some confusing stuff, but I’m starting to see the potential.
Currying is a technique that lets you define a function taking multiple arguments as a series of functions that take one argument, and returns a function that takes the rest. You can define it all the way to the last argument.
This lets you call a funciton with fewer arguments than it takes, and fill in the extras later. For instance, in SML, assuming powint takes two ints and returns the first int to the power of the second:
val p = powint 2
This makes p a function taking one argument, and giving you back the corresponding power of two. Oh, you want powers of 10?
val p10 = powint 10
Next call, you have to do 3^5? You can call it with both arguments right away if you want.
val p3_5 = powint 3 5
I’m still a bit confused on working with this, but the potential is very nice.