Why is it better to have 100 functions working on the same data structure than 10 functions on the 10 data structure

I have seen this in many places:

β€œIt’s better to have 100 functions running on the same data structure than 10 functions on 10 data structures.” -Alan Perlis

But I never saw this explain why this should be true. Is it just an idea that you should try to get the remaining 9 data structures from the first to avoid data duplication? I feel that I am lacking in context.

+52
data-structures quotations
May 16 '11 at 10:47
source share
2 answers

Quote from Alan Perlis The Epigrams for Programming , which was published in 1982.

The meaning of this quote is well embodied in Lisp , where there are many functions that act and are specific to lists, and you could do a lot with lists and a set of functions that work in lists, which makes them much more powerful than any targeted structure data.

Lua , as another example, uses tables to model classes . Why use a table to create objects instead of creating language-level classes and objects, such as object-oriented languages? Since your object is now a table, you can use any number of functions defined for the tables on your object for free! However, we did not have to clutter the language with the class syntax, and we needed to redefine the functions from the table that we want for our class.

What Perlis said is definitely an outstanding way of thinking in Lisp and functional programming . These 100 functions in your one data structure can be combined in many unique ways, since they all work with the same data structure, but you also cannot mix 10 functions with 10 data structures, since they were defined only to work on your specific data structure.

A more modern and simpler variation of this is thinking in terms of abstractions. If we were coding in Java, you would prefer to write 100 functions in the List interface or the same set of ten functions, once for ArrayList, once for LinkedList, once for ....

+69
May 28 '11 at 6:45
source share

The structure and interpretation of computer programs (SICP) answers your question, as shown below:

Screenshot from SICP

You can see the original contents of the online version of the book here.

EDIT (included in comment):

"In Pascal, many declared data structures induce specialization within functions." Specialization is bad because it suppresses "noiselessness" / creativity - I would say - in your own words.

In other words, if functions are too special, then they cannot be reused in ways that were not known at the time the function was created.

A good example is fold ( https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Foldable.html ), which is an agnostic data structure, a common higher-order function. It can be used on a tree, for example

 data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a). 
+6
Aug 24 '14 at 15:04
source share



All Articles