Cover of Structure and Interpretation of Computer Programs, second edition by Harold Abelson, Gerald Jay Sussman - Business and Economics Book

From "Structure and Interpretation of Computer Programs, second edition"

Author: Harold Abelson, Gerald Jay Sussman
Publisher: MIT Press
Year: 1996
Category: Computers

🎧 Free Preview Complete

You've listened to your free 10-minute preview.
Sign up free to continue listening to the full summary.

🎧 Listen to Summary

Free 10-min Preview
0:00
Speed:
10:00 free remaining
Chapter 2: Building Abstractions with Data
Key Insight 2 from this chapter

Implementing 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 ' creates a rational number, while 'numer ' and 'denom ' extract its numerator and denominator. Using these abstract procedures, arithmetic operations like 'add-rat' can be expressed: '(make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))', without needing to know the underlying data structure of 'x' or 'y'.

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.