Functional Programming- Currying


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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: