1 | // repesent, and deal with rational number |
1 | function make_rati(n, d) // make rational number which equal n / d |
1 | function add(a, b) { |
1 | function pair(head, tail) // construct a pair with head and tail |
1 | // handle positive and negative, we only change make_rati |
In general, the underlying idea of data abstraction is to identify for each type of data object a basic set of operations in terms of which all manipulations of data objects of that type will be expressed, and then to use only those operations in manipulating the data.
The horizontal lines represent _abstraction barriers_ that isolate different "levels" of the system. The barriers will hide the implement details below it. All complex data can be repesent by some ways using primitive data. How to repesent data influnences the programs that operate it. But if we only modify the implementation that will not affect the programs build upon it. The data-abstraction methodology gives us a way to defer that decision without losing the ability to make progress on the rest of the system. (For example, fe no need to wait until the be api finished, fe can start develop whenever the api document(interface) is given).Exercise 2.2
1 | // from top to bottom, using data-abstraction barriers to construct the system |
1 | interface { |
1 | // fulfill: z = pair(x, y) head(z) = x tail(z) = y |
1 | function tail(z) { |
1 | function pair(a, b) { |
1 | const zero = f => x => x |
1 | function make_interval(x, y) { |
1 | function sub_interval(x, y) { |
1 | // proof: width A + width B = width C, where C is A + B (interval) |
1 | function div_interval(x, y) { |
1 | function mul_interval(x, y) { |
1 | function make_center_percent(c, p) { |
1 | // a * b -> pa * pb |
If we only see two endpoints, some problems will become harder to solve. But if we change aspect to see data, it will be come eaiser.
sicpjs — May 2, 2022
Made with ❤ and at Earth.