Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

You should see that the foldl version runs significantly faster than the foldr version. And if you comment out foldr and foldl one at a time, try increasing the size of "ints" by a factor of 10, you will see that foldl runs with much less memory than foldr. Why do you think that I wrote "make-ints" as a forward accumulation algorithm?

Delegation Model Programming

Loking beyond the bounds of functional programming, there is an important viewpoint on the accumulation process that becomes increasingly useful as the size of your programs scale upwards.  Consider this comparison between reverse and forward accumulation:

  • In reverse accumulation, you delegate to a function that processes the rest of the list.   That function then returns a value (the accumulator) that you then use to complete your processing of the current data (first) and thus create the new accumulator value, (the return value, which is the completed result).
  • In forward accumulation, you delegate to a function that processes the rest of the list.   That function takes the partially processed data (the new accumulator, formed from first and the previous accumulator value) and completes the processing, returning the completed result.

Both processes can be expressed in terms of a delegation to the rest of the list.   The only difference is what the function on the rest of list is asked to do.  

In "delegation model programming", the notion is that the overall processing of data involves two main factors:  the local processing of local data, e.g. the first of a list, and the delegated processing of non-local data, e.g. the rest of the list.   Processing never crosses encapsulation barriers, so to the processing of encapsulated data, e.g. the data contained in the rest of the list, is always done via a delegation to another function.  

When we move to object-oriented programming, the delegation model viewpoint will become the major mode in which we break down the processing of data between objects, which are amongst other things, encapsulated data.  Specifically, the overall processing of a linked structure of objects will involve the delegation from one object to another.