Lambda calculus is actually pretty simple, and not that useful on its own. However it is when you start learning the implications of lambda calculus and strategies of application control using functional design patterns that you will become a better and more powerful programmer.
The premise is that functions are also values and this makes it really powerful for abstracting many concepts. Your two examples show the simplest way that closures and currying are implemented. Those are trivial examples. When you begin to program using a functional style, those pattern will come up over and over again as very powerful ways to abstract away complexity.
seperation of concerns
Functional programming is useful is when you start abstracting using higher order functions, the typical example being - I want to do something to a collection of objects.
so in an imperative program, you use a for loop:
result = Array(length);
for(i = 0; i < length; i++)
{
result[i] = dosomething(input[i]);
}
notice that in the block of code, the dosomething
function is right in the middle of the for loop. You can't really separate out what you are doing to each element of the array from the actual for-loop control structure.
However, by using a functional style, the control structure of the loop is abstracted away using map
- a higher order function - you get clear separation of two concepts of looping and function application. This is the equivalent code in scheme.
(define result (map dosomething collection))
map
takes care of the fact that you are traversing each element of the array. dosomething
takes care of the fact that you are performing an operation on each element of the array. Reasoning about this becomes much more clear and changing the code becomes much easier. Now imagine that every for
loop in your code was replaced by this construct and how many lines of code it will save.
cheap constructors
Addressing the concept of closures and currying. There is an equivalence between objects and functions. Essentially, functions can be made to look and behave like objects. Javascript makes great use of this fact. Anything you can do with objects, you can also do with functions. In fact, by getting rid of the distinction between what an object is and what a function is, you have effectively decluttered your mind and have one way of thinking about the problem - that is, through code that is built up through functionality.
I am using clojure code here because it is what i'm comfortable with:
This is the clojure code for trivial adder example, With the adder example, you are calling a addn
with the number 5 to get back function that adds 5 to a number. a general use case may be:
(defn addn [n] (fn [m] (+ m n))
(def add5 (addn 5))
(map add5 [1 2 3 4 5 6])
;; => [6 7 8 9 10 11]
Say you had a function download-and-save-to
that takes a url and a database, saves the url to a database table and returns true when it is successful, you can do exactly the same abstraction as you did with +
:
(defn download-url-to [db]
(fn [url] (download-and-save-to url db)))
(def download-url-mydb (download-url-to mydb))
(map download-url-mydb [url1 url2 url3 url4 url5])
;; => [true true false true true]
Notice that the structure is equivalent even though you are doing vastly different things. Think about how you would have approach this problem using java or c++. There would be a lot more code involved in class definitions, factory methods, class abstractions, inheritence.... essentially. you have to go through a whole lot more ceremony to get the same effect.
opening your mind
Because of the way you can express ideas so succinctly, functional programming can let you grasp many concepts that are very difficult to express in more verbose languages. The tool you pick will make exploring certain problems easier or harder.
I highly recommend clojure - (http://www.4clojure.com/) and lighttable - (http://www.lighttable.com/, http://www.kickstarter.com/projects/ibdknox/light-table) to get started. From my personal experience, my one year of learning and exploring concepts by the clojure community was akin to about 10 years of learning java, c++ and python combined.
Oh, did I mention how much fun I'm having learning all this stuff? Monads, combinators, propagators, continuations... all those scary looking academic concepts are actually well within reach when you have the right tools for understanding them.