

Explaining Functional Programming to Eight-Year-Olds - ekiru
http://prog21.dadgum.com/73.html

======
cabalamat
I think the difficulty in explaining this is that you have to "inject" a value
in at the start (indeed the Smalltalk equivalent is called inject:). Without
injection, the function amounts to:

    
    
       a1 op a2 op a3 op ... op an
    

where op is some binary operation. Executing op can either start from the left
or right, so you have a left-fold or right-fold respectively. In Unify ATM
I've defined the functions:

    
    
       (fold iv ar f)
       (foldr ar iv f)
       (foldn ar f)
       (foldrn ar f)
    

Where: -r = right fold, -n = no injected value ar = an array (Unify uses
arrays not lists) f = a function taking 2 arguments iv = an injection value

Although it might be better to cut the functions down to 2, and make iv an
optional argument:

    
    
       (fold f ar vararg iv)
       (foldr f ar vararg iv)
    

Alternately I could use 'reduce' for the injectionless, and 'inject' for the
injectionful versions of the function.

~~~
aufreak3
left-fold and right-fold are not the only two. If you're dealing with
commutative and associative operators, you can group them up in a binary (or
n-ary) hierarchy for parallel execution as well ... and is there a name for
that?

map is obviously parallelized in a purely functional language. My opinion is
that fold, otoh, is better limited to use with commutative and associative
operators for clarity and transparent parallelization. That way, you don't
have to think about whether you need to use a left or a right fold, or indeed
a parellel fold.

Other "iterations" can be expressed using explicit recursion using an
accumulator ... again, for clarity.

~~~
dkersten
_and is there a name for that?_

In Clojure its _preduce_ , I believe (for parallel reduce; in Intel Threading
Building Blocks its called parallel_reduce).

~~~
silentbicycle
In k, a similar operation is called "peach", for "parallel each", though (as
with J and APL) most of the operations already have an implicit map/loop
built-in.

------
rgrieselhuber
I'm pretty sure I wasn't smart enough to understand that when I was eight
years old.

~~~
rflrob
What really bothers me about this article is that at no point are there
actually any eight year-olds. It's one thing to say "An eight year-old could
understand this", and another to have a loose transcript of an actual
conversation, à la "Explaining REST to my Wife" (
<http://tomayko.com/writings/rest-to-my-wife> ) or "The Socratic Method",
where the author teaches binary to third graders (
<http://www.garlikov.com/Soc_Meth.html> )

------
chc
The thing about folds is that they build upon more fundamental concepts. If
you understand recursion — which you must if you want to do FP — they're not
too weird. But most people have trouble thinking recursively, so the simple
implementation of a fold is confusing.

I think the name doesn't help either. People understand "reduce" more
intuitively than "fold".

------
rntz
insert _is_ fold, just underspecified (what happens on 1- or 0-element lists?
in what direction does the operator associate?). The only difference in
comprehensibility is in the language being used to describe the operation -
which is certainly a good thing to take note of if you're trying to teach FP,
but it's not as if the operation itself is somehow less-comprehensible.

