From "Structure and Interpretation of Computer Programs, second edition"
🎧 Listen to Summary
Free 10-min PreviewImplementing Data Abstraction and Abstraction Barriers
Key Insight
To implement a system using data abstraction, such as rational number arithmetic, one defines operations (like addition, subtraction, multiplication, division, equality testing) in terms of a constructor and selectors. For instance, 'make-rat
The primitive 'pair' provides the necessary 'glue' for concrete data representation. A pair is constructed using 'cons' (e.g., '(cons 1 2)') and its parts extracted using 'car' (e.g., '(car x)' yields 1) and 'cdr' (e.g., '(cdr x)' yields 2). With pairs, 'make-rat' can be implemented as '(cons n d)', 'numer' as '(car x)', and 'denom' as '(cdr x)'. This concrete representation allows for displaying rational numbers (e.g., '1/2') and performing operations like '(add-rat (make-rat 1 2) (make-rat 1 3))' to yield '5/6'.
A crucial design principle is the 'abstraction barrier', which separates program parts that use data from those that implement it. In the rational number system, 'add-rat' and similar operations only interact with 'make-rat', 'numer', and 'denom', which in turn interact with 'cons', 'car', and 'cdr'. This modularity allows for changes in the underlying representation, such as incorporating a 'gcd' procedure into 'make-rat' to reduce fractions to lowest terms (e.g., '(make-rat 6 9)' becomes '2/3'), without requiring any modifications to the higher-level arithmetic procedures like 'add-rat'. Abstraction barriers enhance maintainability, simplify modification, and enable deferring implementation decisions.
📚 Continue Your Learning Journey — No Payment Required
Access the complete Structure and Interpretation of Computer Programs, second edition summary with audio narration, key takeaways, and actionable insights from Harold Abelson, Gerald Jay Sussman.