Summary of problems solved by the SICP exercise (2.3), sicp2.3

Source: Internet
Author: User

Summary of problems solved by the SICP exercise (2.3), sicp2.3

Exercise 2.3 requires us to implement a representation of a plane rectangle and define the selection function for getting data. Then define several processes to calculate the circumference and area of the rectangle.

Then the question also requires us to implement another expression of the rectangle, and requires that this new rectangular representation also applies to the calculation of the circumference and area defined above.


We can implement this question from top to bottom, and the implementation process is not complicated. The reason is that the mathematical concept involved in this question is still relatively simple, that is, the area and perimeter of the rectangle, it's almost our primary school knowledge. However, after the question, we need to implement the different representation of the rectangle and support the same process of calculating the circumference and area. This is a little troublesome. In fact, this question is a bit stolen, some concepts involved are described in detail in later chapters of this book. If we only use some of the Scheme technologies we have learned, we can only partially implement this question.


First, we start to implement the area calculation process and perimeter calculation process.


The area calculation process is "length multiplied by width". The Code is as follows:


(define (area-rect rect)  (* (get-length rect) (get-width rect)))

The circumference calculation process is "Length plus width, and then multiply by 2". The Code is as follows:

(define (perimeter-rect rect)  (* 2 (+ (get-length rect) (get-width rect))))


Through the above process, we can find that we need to implement the get-length and get-width processes to obtain the length and width of a rectangle.


The specific implementation of get-length and get-width depends on how the rectangular data structure is implemented.

I will list my first implementation below. The implementation method is to define a rectangle through the coordinates of the two vertices of the rectangle. The Code is as follows:


(define (make-rectangle-1 corner-1 corner-2)  (cons corner-1 corner-2))


Based on our experience in the last two questions, we know that we need to define two functions to obtain the two vertices of a rectangle. The Code is as follows:

(define (corner-1 rectangle)  (car rectangle))(define (corner-2 rectangle)  (cdr rectangle))


After we can obtain the data of two vertices of a rectangle, we can define the get-length and get-width processes. The Code is as follows:

(define (get-length rectangle)    (let ((length1 (abs (- (point-x (corner-1 rectangle)) (point-x (corner-2 rectangle)))))  (length2 (abs (- (point-y (corner-1 rectangle)) (point-y (corner-2 rectangle))))))      (if (> length1 length2)  length1  length2)))(define (get-width rectangle)  (let ((width1 (abs (- (point-x (corner-1 rectangle)) (point-x (corner-2 rectangle)))))(width2 (abs (- (point-y (corner-1 rectangle)) (point-y (corner-2 rectangle))))))    (if (> width1 width2)width2width1)))


The above code is relatively simple, that is, through the conner-1 and conner-2 process to obtain the vertex data of the rectangle, and then through point-x and point-y to obtain the x and y coordinates of These vertices, the length and width of the rectangle can be obtained through the difference between the x coordinate of the two vertices and the y coordinate.


The above implementation is relatively direct, similar to the previous questions, and the problem is somewhat troublesome, that is, to define another way of representing the rectangle, calculate the perimeter of a rectangle using the same area and perimeter.


It is relatively simple to define a rectangle by using another representation. For example, we can define a rectangle by using a vertex of the rectangle and the data of the length and width.

The code for constructing such a rectangle is as follows:

(define (make-rectangle-2 corner width length) (cons corner (cons width length)))


The above code is to use the cons process to connect the vertex, length, and width.


Corresponding to this implementation, the code for getting the length and width of the rectangle is very direct:

(define (get-length rectangle)    (abs (cdr (cdr rectangle))))(define (get-width rectangle)    (abs (car (cdr rectangle))))



Because the get-length and get-width processes are also implemented, we can find that the rectangle made by make-rectangle-1 and make-rectangle-2 can both calculate the area through the area-rect process, or use the perimeter-rect process to calculate the perimeter.


That is to say, we use get-length and get-width to hide the implementation details of the rectangle. No matter what form is used to implement the data structure of the rectangle, because the interfaces for obtaining the length and width are the same, both area-rect and perimeter-rect can correctly calculate the area and perimeter of the rectangle.


However, there is a problem here, that is, make-rectangle-1 and make-rectangle-2 correspond to different get-length and get-width processes, respectively, in Scheme, we do not see the implementation of "method overloading". In a runtime environment, only one set of get-length and get-width are allowed. That is to say, the implementation methods of the two rectangles we define cannot coexist in one environment.


If our goal is only to meet the requirements of the question, the above Code can be completed, we define two rectangular implementations, they can use the same area and perimeter process to calculate the area and perimeter.

However, as a self-respecting programmer, the above Code seems unacceptable. The two rectangle implementations cannot even run in the same system. What is this.


Once you take this into consideration, you will find that this is not a simple solution. Here it involves the concept of "method overloading", which is a direct point, that is, we define two or more methods with the same name (or functions with the same name). They can do different things according to different parameter types. The approximate code is as follows:

(Define (catch fish) (display "catch fish on the Internet") (define (catch rabbit) (display "find a tree pile to wait") (define (catch tiger) (display "catch what to catch "))



There are three catch methods. When the input parameters are "fish", "rabbit", and "tiger", they do different work.

Of course, the above Code does not work in Scheme as we hope. In Scheme, only the (catch tiger) function is valid. Because Scheme does not support process overloading, only the last defined process takes effect.


If we consider implementing "method overloading", we will further find that there are still many problems to solve. For example, we need to determine the parameter type, that is, to determine that the parameter is "fish ", rabbit or tiger ". To achieve this, we need to further introduce the concept of "type" to determine whether the input parameter belongs to "fish", "rabbit", or "Tiger ", for example, the following code calls the fish capture process:

(Catch "salmon") (catch "big head fish ")

Therefore, we need to determine that the "type" of "salmon" is "fish", and finally decide to call the fish capture process.


Further, to determine the "type" of the "salmon", we need to add the "type" item to the data structure that stores the "salmon.


In languages that support process overloading, the determination of parameter types and the selection of corresponding processes are implemented at the underlying layer, which developers do not need to know.

However, for our current learning progress, the language-level implementation process overload is too advanced, and we will give a detailed description in the chapters later on, in fact, there are different implementation methods.


Therefore, in the process of completing this question, we still use some simple methods, maybe some poor methods, to directly complete the work through the type judgment.


Back to our rectangle definition, the first thing we need to do is to add "type" to the data structure of the rectangle ". Considering the data structures of different types of rectangles are different, it is obviously a good method to place the data of "type" in the first position. In this way, you can take the first value after obtaining the rectangle data to determine the type of the rectangle.

The corresponding code is as follows:

(define (make-wrapped-rectangle-1 corner-1 corner-2)  (cons 'type-1 (make-rectangle-1 corner-1 corner-2)))(define (make-wrapped-rectangle-2 corner width length)  (cons 'type-2 (make-rectangle-2 corner width length)))



In this way, to get the type and positive content from "wrapped-rectangle", we need to define the corresponding retrieval method. The Code is as follows:

(define (type rectangle)  (car rectangle))(define (content rectangle)  (cdr rectangle))

With the new rectangular data structure, we can define our new get-width and get-length processes:

(define (get-length rect)  (cond ((eq? (type rect) 'type-1) (get-length-1 (content rect)))((eq? (type rect) 'type-2) (get-length-2 (content rect)))(else (error "incorrect data type" rect))))(define (get-width rect)  (cond ((eq? (type rect) 'type-1) (get-width-1 (content rect)))((eq? (type rect) 'type-2) (get-width-2 (content rect)))(else (error "incorrect data type" rect))))



The above code is more direct, that is, to obtain the "type" of the rectangle. If it is type-1, call the process of "get-width-1" and "get-length-1, for type-2, call the get-width-2 and get-length-2 processes.


Correspondingly, We need to rename the two previously implemented get-width procedures to get-width-1 and get-width-2.

The get-length process is the same, and the code is as follows:

(define (get-length-1 rectangle)    (let ((length1 (abs (- (point-x (corner-1 rectangle)) (point-x (corner-2 rectangle)))))  (length2 (abs (- (point-y (corner-1 rectangle)) (point-y (corner-2 rectangle))))))      (if (> length1 length2)  length1  length2)))(define (get-width-1 rectangle)  (let ((width1 (abs (- (point-x (corner-1 rectangle)) (point-x (corner-2 rectangle)))))(width2 (abs (- (point-y (corner-1 rectangle)) (point-y (corner-2 rectangle))))))    (if (> width1 width2)width2width1)))(define (get-length-2 rectangle)    (abs (cdr (cdr rectangle))))(define (get-width-2 rectangle)    (abs (car (cdr rectangle))))


In this way, we have basically finished our work.


Of course, as mentioned above, this is not a clever method, but you do not need to worry about it. More methods will be discussed in the following questions, and there is no lack of clever methods.


Continue to cheer!




Answers to exercises in the ninth grade mathematics book 23th (do not tell me the answer in reference books)

People's Education Press
Www.pep.com.cn
Junior high school question Network
Www.czst.cn/soft_index.asp
Course home
Www.kejianhome.com
Look for it carefully and hope it will help you

, 123 question solving process and answer

A. Enter the value of x. 1-3 m + 2 = 0. That is, 3 = 3 m; m = 1, then x = 1 = m. So x-m = 1-1 = 0
A. From the property of the absolute value and the definition of the square, 3a + B + 5 = 0 is obtained. Then 2a-2b-2 = 0, 2a-2b = 2 are divided by 2 at the same time, and the original question a-B = 1 is obtained.
D. This question is relatively simple. The test is to understand and use the equation.
Solution: Set the purchase price of type A headphones to x yuan, and the purchase price of type B headphones to y yuan.
Get x + 60% x = 64 y-20 % y = 64
X = 40, y = 80. The total purchase price for the two headphones is 120 yuan, and the total price for the two headphones is 128 yuan. In general, I earned 8 yuan.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.