Explaining Functional Programming to Eight-Year-Olds
Programming in the 21st Century - James Hague - July 09, 2010“Map” and “fold” are two fundamentals of functional programming. One of them is trivially easy to understand and use. The other is not, but that has more to do with trying to fit it into a particular view of functional programming than with it actually being tricky.
There’s not much to say about map. Given a list and a function, you create a new list by applying the same function to each element. There’s even special syntax for this in some languages which removes any confusion about whether the calling sequence is map(Function, List) or map(List, Function). Here’s Erlang code to increment the values in List:
[X + 1 || X <- List]
Fold, well, it’s not nearly so simple. Just the description of it sounds decidedly imperative: accumulate a result by iterating through the elements in a list. It takes three parameters: a base value, a list, and a function. The last of these maps a value and the current accumulator to a new accumulator. In Erlang, here’s a fold that sums a list:
lists:foldl(fun(X, Sum) -> X + Sum end, 0, List)
It’s short, but it’s an awkward conciseness. Now we’ve two places where the parameter order can be botched. I always find myself having to stop and think about the mechanics of how folding works—and the difference between left and right folding, too (lists:foldl is a left fold). I would hardly call this complicated, but that step of having to pause and run through the details in my head keeps it from being mindlessly intuitive.
Compare this to the analog in array languages like APL and J. The “insert” operation inserts a function between all the elements of a list and evaluates it. Going back to the sum example, it would be “+/” in J, or “insert addition.” So this:
1 2 3 4 5 6
turns to this:
1 + 2 + 3 + 4 + 5 + 6
giving a result of 21. The mechanics here are so simple that you could explain them to a class of second graders and not worry about them being confused. There’s nothing about iterating or accumulating or a direction of traversal or even parameters. It’s just…insertion.
Now there are some edge cases to worry about, such as “What does it mean to insert a function between the elements of a list of length 1”? Or an empty list for that matter. The standard array language solution is to associate a base value with operators, like addition, so summing a list containing the single value 27 is treated as 0 + 27. I’m not going to argue that APL’s insert is more general than fold, because it certainly isn’t. You can do all sorts of things with the accumulator in a traditional fold (for example, computing the maximum and minimum values of a list at the same time).
But in terms of raw simplicity of understanding, insert flat-out beats fold. That begs the question: Is the difficulty many programmers have in grasping functional programming inherent in the basic concept of non-destructively operating on values, or is it in the popular abstractions that have been built-up to describe functional programming?
(If you liked this, you might like Functional Programming Archaeology.)
Categories: Blogs Programming in the 21st Century
Comments
insert(F, [H1,H2]) -> F(H1,H2);
insert(F, [H1,H2|T]) -> insert(F, [F(H1,H2)|T]).
so insert/2 should be in the standard libraries?
:-)
Posted by anon on 11 Jul 2010 at 10:47
Add comment
Erlang on Twitter
» hnakamur2 (Hiroaki Nakamura): Erlang/OTPは“a true dream technology”とのことです。まだ仕事で使ったことはないけど、私も同感だなー。
[erlang-questions] The future of Erlang and BEAM http://t.co/QRR5w029
» setyawanSH (setyawan): Mbok ra koyok cah cilik ndra RT @Harindraa: Dewa erlang, dewi kuan’im, paman pikolo, paman kweceng, bibi lung, mbak yoona, mbak seohyun podo
» Harindraa (Haryndra Nugraha): Dewa erlang, dewi kuan’im, paman pikolo, paman kweceng, bibi lung, mbak yoona, mbak seohyun podo ning endi? Aku butuh sandaran :’(
» quantymt (高橋誠(MakotoTakahashi)): えwww!!!、erlangってソースが71MBもあるの?
» bestjobsonline (Best Jobs): Senior Erlang Engineer - relo to SF available - http://t.co/BaKJm1J3 #jobs #CyberCodersEngineering #NewYork
» agnesMRPS (A . M . R . P . S): @doni_erlang ora !
» agnesMRPS (A . M . R . P . S): @doni_erlang tlg y pke bhsa yg merakyat . Aku dk ngerti kw pke bhsa planet mna ? :p
» eComjobs (Henry James): Ecom Jobs USA Senior Erlang Developer - Principal Erlang Engineer - Erlang: IN-Indianapolis, CyberCode… http://t.co/9t8gRwA7 #ecomjobs
» agnesMRPS (A . M . R . P . S): @doni_erlang apaan si ? :p
Statistics
Number of aggregated posts: 10456
Number of comments: 1446
Most recent article: February 06, 2012
Latest comments
» vindisesl on Pretend This Optimization Doesn't Exist: I completely agree with you. I really like this article. It contains a lot of useful information. I can set…
» simple smile on Scale means Skills: Very informative article. Pretty sure people would love to go to that place for shopping. Specially to those who are…
» simplesmile on 27 January 2012: Erlang Solutions embarks on an Erlang Embedded KTP: Your article will make the world better. Thanks again and good luck to you in your life. See you next time.simplesmile