Summary of the Problem Solving of SiC exercise (2.5), sicp2.5
Exercise 2.5 is a bit like a mathematical problem. First, we need to prove that we can represent the ordinal pairs of a and B as 2 ^ a * 3 ^ B, then, the Order pairs are represented by non-negative integers and arithmetic operations. Finally, we need to implement the corresponding cons, car, And cdr processes.
The root of this question is the composition and resolution of composite data. In fact, for all the composite data, we are dealing with the same thing, that is, how to build the composite data components together, and at the same time, they can be disassembled through specific methods.
As if we want to store table tennis and tennis balls, we can distinguish between table tennis and tennis. The simple method is to mix table tennis and tennis. when we need it, we can easily determine which table tennis is needed, which is tennis. Our operations did not lead to the loss of information. Whether a ball is a table tennis or a tennis ball is followed by the ball itself.
On the contrary, if we want to store table tennis from two schools and distinguish between different schools, we cannot simply mix them together. Once we put the two schools together, we lost part of the information. Before mixing, we know that the table tennis in Box A comes from A school, and the table tennis in Box B comes from another school. Once we put them in A box, we lost information about which school the ball belongs.
When we get back to the question, we need to figure out whether we can get the number x when we get 2 ^ a * 3 ^ B = x.
The answer is certainly yes. The specific Mathematics proves that you can go to Baidu. The general meaning is that any integer greater than 1 can be decomposed into a unique prime number multiplication sequence, because our x = 2 ^ a * 3 ^ B, and 2 ^ a * 3 ^ B is a sequence of prime numbers multiplied, that is to say, x can only be divided into 2 and multiplied by * 3 of B. In this way, we only need to look at the number of 2 in x to get a. Similarly, we only need to look at the number of 3 in x to get B.
In fact, it is not always possible to use 2 and 3. You can try it if you are interested.
Okay. Now that we can prove it, let's take a look at how the code is implemented. Speaking of code implementation, it's not just a good trick for our coders!
The cons process is defined as follows, that is, get x through 2 ^ a * 3 ^ B
(define (cons a b) (* (expt 2 a) (expt 3 b)))
The car process is defined as follows, that is, there are several 2 in number x.
(define (car x) (define (iter counter number-left) (if (= (remainder number-left 2) 0)(iter (+ counter 1) (/ number-left 2))counter)) (iter 0 x))
The cdr process is defined as follows, that is, there are several 3 in number x.
(define (cdr x) (define (iter counter number-left) (if (= (remainder number-left 3) 0)(iter (+ counter 1) (/ number-left 3))counter)) (iter 0 x))
In short, we can open up the idea of implementing Composite data, as long as the components of composite data can be decomposed.
At this time, let's go back and look at our common Composite data structure. For example, the struct in C language is actually allocating a space continuously in the memory area, you can stack struct elements one by one. Because we know the Data Type of each element, we know how much memory space each element occupies, in this way, we can simply obtain different elements through the displacement.
In contrast, although the composite data construction method used in this question is not so efficient, it is really a great idea.
However, let's take a look at question 2.6 in the future.
What is the problem solving process? The following four formulas-(-25
A
Rewrite the following equation in proportion (1) 3 × 40 = 8 × 15 (2) 25 × 04 = 05 × 2 (solving the problem of such questions)
=, 2.5: 0.5 =. 4