Introduction
I have been learning Haskell for work recently (quite advanced Haskell if I do say so). People have in the past promoted the idea of learning a functional language (Haskell is nothing like any language like C/PHP/Javascript etc) as even if you never use it it will teach you entirely new ways of thinking about code – and it’s true. I didn’t notice it at first, but I’ve begun to notice one way in which programming in Haskell has affected my thoughts on programming, and that’s in function parameter orders.
In the past I would have almost universally done this:
void SomeFunction(int array[], size_t size);
Declaring a function that takes an array and the size of that array (importantly in that order). Haskell, however, has a feature called “currying” whereby you can pass some parameters to a function and return that partially evaluated function for later (vaguely similar to closures in Javascript):
func1 array size = {- Code goes here -}
func2 = func1 [10, 11, 12, 13, 14]
Here “func1″ has two parameters (“array” and “size” as before), “func2″ has none and calls “func1″ with only 1 parameter. The result is that “func2″ actually returns a function that takes a single parameter, or to put it another way “func2″ takes one parameter, you just don’t need to put it in. Not bothering to write parameters is called “point free” in Haskell – some people hate it, some love it, it serves to illustrate the point above. We could have also written:
func2 size = func1 [10, 11, 12, 13, 14] size
Here it is blatantly obvious that “func2″ takes one parameter and passes two to “func1″, the fact that in both cases the last parameter is “size” is what allows us to hide it.
(more…)