3

From the reference of berkeley's version of sicp text, It is mentioned that:

Expressing programs as sequence operations helps us design programs that are modular. That is, our designs are constructed by combining relatively independent pieces, each of which transforms a sequence. In general, we can encourage modular design by providing a library of standard components together with a conventional interface for connecting the components in flexible ways.

For instance, In python, <class 'list'> and <class 'tuple'> are some of the types of mostly used sequential data models for storage.

For instance, In python <class 'dict'> is one popular type of non-sequential data model.

Text reference gives below solutions written in python to two different problems and mentions as similar solution, which is not convincing, because these two problems could not be the representative sample to take this decision.

>>> def sum_even_fibs(n):
        """Sum the even members of the first n Fibonacci numbers."""
        return sum(filter(iseven, map(fib, range(1, n+1))))
>>> sum_even_fibs(20)
3382

>>> def acronym(name):
        """Return a tuple of the letters that form the acronym for name."""
        return tuple(map(first, filter(iscap, name.split())))
>>> acronym('University of California Berkeley Undergraduate Graphics Group')
('U', 'C', 'B', 'U', 'G', 'G')

I am still not clear on the decision to make "sequences as conventional interfaces" despite these given examples.

So, Amidst designing a library or component, Why sequential type data models are recommended to be chosen as conventional interfaces?Is this something to do with closure property of these sequential data models(provided by python/scheme/lisp)?

Note: Same recommendation is given in SICP text from MIT press

overexchange
  • 2,315

1 Answers1

7

The purpose of a dictionary is to return a value given a specified key, not to serve as a sequential container. Just because other data structures are sequential but dictionaries are not does not mean that the benefits of sequences are invalidated.

Sequences are one of the three fundamental logic structures in computing. Every (solvable) computing problem can be solved with three tools: sequences, loops, and decisions.

Sequences preserve the relationship between elements; that is, the first element must occur before the second element, which must occur before the third element, and so forth. That means we can retrieve the next element in a sequence, and be assured that we retrieved the element in the correct order, relative to the other elements. This has obviously useful benefits: words always consist of the same sequence of letters each time they are read, and methods consist of the same sequence of instructions each time they are called.

You can make sequences lazy. You can create a class that implements a state machine, and returns an element from the state machine each time a method is called. You can cache sequences.

Sequences can be created from linked lists, trees, and other data structures. Each data structure has its own specific qualities; a singly-linked list can be efficiently traversed in one direction, but not the other. Some data structures, such as binary trees, will give you sequential access and also efficient random access. Some data structures, such as doubly-linked lists, allow you to efficiently traverse the sequence backwards.

If you write your data structure to conform to a sequential interface, you can switch out your implementation of the sequence to whichever data structure is most appropriate for your given requirements. You can then write your code against that interface, and be assured that the underlying implementations are modular.

I don't know much about sequences in python, but the fundamental data structure in Scheme (used in the original SICP) is a list, and the ways in which you can compose such lists (which are essentially sequences), and the flexibility and power that affords, should be obvious. Every language containing sequences allows such compositions.

Robert Harvey
  • 200,592