Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it better to have 100 functions operate on one data structure than 10 functions on 10 data structure

I have seen this quoted in a lot of places:

"It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." —Alan Perlis

But I have never seen it explained why this should be true. Is it just the idea that you should try to derive the other 9 data structures from the first to avoid duplicating the data? I feel like I'm missing some context.

like image 723
GlennS Avatar asked May 16 '11 10:05

GlennS


People also ask

Why do we need different data structures?

A data structure is a specialized format for organizing, processing, retrieving and storing data. There are several basic and advanced types of data structures, all designed to arrange data to suit a specific purpose. Data structures make it easy for users to access and work with the data they need in appropriate ways.

What is the main advantage of data structure?

Advantages:- 1) Allows easier processing of data. 2) It allows information stored on disk very efficiently. 3) These are necessary for designing an efficient algorithm. 4) It provides management of databases like indexing with the help of hash tables and arrays.

Why is it important to know data structures in solving problems?

Data structures and algorithms (DSA) goes through solutions to standard problems in detail and gives you an insight into how efficient it is to use each one of them. It also teaches you the science of evaluating the efficiency of an algorithm. This enables you to choose the best of various choices.


2 Answers

The quote is from Alan Perlis' Epigrams on Programming, which was published in 1982.

The meaning of this quote is embodied well in Lisp, where there are multitudes of functions that operate and deal specifically with lists, and you could accomplish a lot just with lists and the assortment of functions that operate on lists, which makes them much more powerful than any single-purpose data structure.

Lua, as another example, uses tables to simulate classes. Why use a table to make objects instead of creating language-level classes and objects like object-oriented languages do? Since your object is a table now, you can use any number of the functions defined for tables on your object, free of charge! Better yet, we didn't have to clutter the language with class-specific syntax and have to redefine functions from table that we want for our class.

What Perlis said is definitely a prominent mode of thought in Lisp and in functional programming in general. Those 100 functions on your one data structure can be composed together in lots of unique ways, since they all operate on the same data structure, but you can't really mix the 10 functions on 10 data structures as well, since they were defined only to work on their particular data structure.

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

like image 70
Zach L Avatar answered Oct 11 '22 13:10

Zach L


Structure and Interpretation of Computer Programs (SICP) answers your question as below:

Screenshot from SICP

You can see the original content of the online version of the book here

EDIT (included from comment):

"In Pascal the plethora of declarable data structures induces a specialisation within functions." Specialisation is bad because it inhibits "serendipity"/creativity - I would say - with my own words.

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

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

data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a). 
like image 28
jhegedus Avatar answered Oct 11 '22 13:10

jhegedus