Culture shock

Torbjorn Tornkvist <>
Sat Dec 2 20:43:39 CET 2000


An explanation of functional programming I've tried
on students is to compare a function with a black-box
containing some machinery which takes some input (via
the input arguments) and produce a result (the returned
result). 

  Example 1:

              X   Y
              |   |
           +---------+
           |  plus/2 |      X+Y=Z
           +---------+
                |
                Z

By combining 'boxes' you form larger boxes and voila,
you have your program written.

 Example 2:
 
              X   Y      W
              |   |      | 
      +-------+---+------+----+
      |       |   |      |    |
      |    +--+---+--+   |    |
      |    |  plus/2 |   |    |
      |    +----+----+   |    |
      |         |        |    |
      |         |   +----+    |  plus/3 == X+Y+W=Z
      |         |   |         |
      |     +---+---+-+       |
      |     |  plus/2 |       |
      |     +----+----+       |
      |          |            |
      +----------+------------+
                 |
                 Z

So I guess, what I want to say is: think in boxes
instead of control flow structures.

BTW: Try to follow the rules:

 1. The only way the affect the behaviour of a function
    is via the input arguments.

 2. The only way for a function to affect its surrounding
    environment is via the returned result.

 3. The same input will always give the same result.

 4. Try to restrict the code that breaks rule 1,2,3
    into one place.

Just my naive thoughts on the topic (a Saturday night... :-)

Cheers /Tobbe




More information about the erlang-questions mailing list