Learning notes for the publication (2.3.1 ~ 2.3.2)

Source: Internet
Author: User

Learning notes for the publication (2.3.1 ~ 2.3.2)

Zhou yinhui

 

1. Practice 2.53

(Define (memq2 item x)
(Cond (null? X) # f)
(Eq? Item (car x)
(Else (memq item (cdr x )))))

 

(List 'A' B 'c)

(List 'George ))
(Cdr '(x1 x2) (y1 y2 )))

(Cadr' (x1 x2) (y1 y2 )))
(Pair? (Car '(a short list )))
(Memq 'red' (red shoes) (blue socks )))

(Memq 'red' (red shoes blue socks ))

The running result is:

(A B c)
(George ))
(Y1 y2 ))
(Y1 y2)
# F
# F
(Red shoes blue socks)

 

2, eq? Eqv? And equal?

First, equal? True when and only when eqv? True; eqv? True when and only when eq? True.
Second, when two variables reference the same object, is eq? True, otherwise false
Again, eqv? And eq? Different values may be returned only for the Number and Character types.
Finally, when eqv? When # f is returned, if the type implements the equal + hash attribute, the method in this attribute is called for object comparison.
In general, we can understand: eq? Is it to determine whether two parameters point to the same object; equal? It is to determine whether two objects have the same structure and whether the content in the structure is the same

 

3. Practice 2.54

The custom list comparator. To avoid conflicts with the system's equal, the name is called limit 2. The Code is as follows:

(Define (Limit 2? A B)
(Cond (and (null? A) (null? B) # t)
(Null? A) # f)
(Null? B) # f)
(Else (and (eq? (Car a) (car B ))
(Limit 2? (Cdr a) (cdr B ))))))

; Test
(Limit 2? '(This is a list ))
(Limit 2? '(This is a list)' (this (is a) list ))

To explain a little, for two lists a and B:
If a and B are empty, they are equal. Otherwise, at least one of a and B is not empty,
At this time, if a is null (then B must not be empty), then a and B are not equal,
Otherwise, if B is null (a is not empty at this time), then a and B are not equal,
Otherwise, (both a and B are not empty at this time), whether a and B are equal depends on whether (car a) and (car B) are equal and (cdr) is it equal to (car B )?

 

4. Practice 2.55

The quote is output for (car 'a) because its essence is interpreted as: (quote ))

The following code illustrates the problem:

Code

(Define a (quote something )))
(Define B ''something)

; Test
(Equal? A B)
(Car a); extracts the second quote from (quote something)
(Car B)

 

5. Practice 2.56
For power function derivation, We can first define the constructor of the power function expression, and then define other selection functions: judge whether an expression is a power function and evaluate the base number, evaluate the power index and the derivative ....

; Construct a power expression
(Define (make-exponentiation x y)
(Cond (or (= number? X 1) (= number? Y 0) 1)
(= Number? Y 1) x)
(= Number? X 0) 0)
(And (number? X) (number? Y ))
(* X (make-exponentiation x (-y 1 ))))
(Else (list '^ x y ))))

; Determine whether it is a power
(Define (exponentiation? Exp)
(And (pair? Exp) (eq? (Car exp) '^ )))

; Evaluate the power base number
(Define (base exp) (cadr exp ))

; Power index
(Define (exponent exp) (caddr exp ))

In make-exponentiation, x indicates the base number, and y indicates the power index. If x is 1 or y is 0, the value of the entire expression is 1. If y is 1, the expression value is equivalent to the value of x. If x is 0, the expression value is 0. If x and y are other common values, then the expression value is a concatenation of y x (recursively expressed here), otherwise we will represent it in the form of (^ x y)
According to the index derivation rule (x ^ n) '= n * (x ^ (n-1), the function is written as follows:

; Evaluate the derivative (not including the derivation of Division)
(Define (derivation exp var)
(Cond (number? Exp) 0)
(Variable? Exp)
(If (same-variable exp var) 1 0 ))
(Sum? Exp)
(Make-sum (derivation (addend exp) var)
(Derivation (augend exp) var )))
(Product? Exp)
(Make-sum
(Make-product (multiplier exp)
(Derivation (multiplicand exp) var ))
(Make-product (multiplicand exp)
(Derivation (multiplier exp) var ))))
(Exponentiation? Exp)
(Let (expt (exponent exp ))
(Bs (base exp )))
(Make-product
(Make-product expt
(Make-exponentiation bs (-expt 1 )))
(Derivation bs var ))))
(Else
(Error "error ~ "))))

The complete code is here:

Click expand/collapse code

; Test whether it is a simple variable
(Define (variable? X) (symbol? X ))

To test whether a variable is equal to a certain number.
(Define (= number? X)
(And (number? X) (= x )))

; Test whether two variables are equal
(Define (same-variable x y)
(And (variable? X) (variable? Y) (eq? X y )))

; Construct a power expression
(Define (make-exponentiation x y)
(Cond (or (= number? X 1) (= number? Y 0) 1)
(= Number? Y 1) x)
(= Number? X 0) 0)
(And (number? X) (number? Y ))
(* X (make-exponentiation x (-y 1 ))))
(Else (list '^ x y ))))

; Determine whether it is a power
(Define (exponentiation? Exp)
(And (pair? Exp) (eq? (Car exp) '^ )))

; Evaluate the power base number
(Define (base exp) (cadr exp ))

; Power index
(Define (exponent exp) (caddr exp ))

; Constructor and Formula
(Define (make-sum x y)
(Cond (= number? X 0) y)
(= Number? Y 0) x)
(And (number? X) (number? Y) (+ x y ))
(Else (list '+ x y ))))

; Judge whether the expression is a sum
(Define (sum? X)
(And (pair? X) (eq? (Car x) '+ )))

; Addition in summation
(Define (addend exp) (cadr exp ))

; Number of sheets in the summation form
(Define (augend exp) (caddr exp ))

; Constructor
(Define (make-product x y)
(Cond (or (= number? X 0) (= number? Y 0) 0)
(= Number? X 1) y)
(= Number? Y 1) x)
(And (number? X) (number? Y) (* x y ))
(Else (list '* x y ))))

; Determine whether the expression is a multiplication
(Define (product? X)
(And (pair? X) (eq? (Car x )'*)))

; Calculate the multiplier in the multiplication Formula
(Define (multiplier exp) (cadr exp ))

; Calculate the multiplier in the multiplication Formula
(Define (multiplicand exp) (caddr exp ))

; Evaluate the derivative (not including the derivation of Division)
(Define (derivation exp var)
(Cond (number? Exp) 0)
(Variable? Exp)
(If (same-variable exp var) 1 0 ))
(Sum? Exp)
(Make-sum (derivation (addend exp) var)
(Derivation (augend exp) var )))
(Product? Exp)
(Make-sum
(Make-product (multiplier exp)
(Derivation (multiplicand exp) var ))
(Make-product (multiplicand exp)
(Derivation (multiplier exp) var ))))
(Exponentiation? Exp)
(Let (expt (exponent exp ))
(Bs (base exp )))
(Make-product
(Make-product expt
(Make-exponentiation bs (-expt 1 )))
(Derivation bs var ))))
(Else
(Error "error ~ "))))

; Test
(Derivation '(* x y) (+ x 3) 'x)
(Derivation '(* (^ x 5) y) 'x)

 

Output result:
(+ (* X y) (* (+ x 3) y ))
(* Y (* 5 (^ x 4 )))

 

6. Practice 2.57
If the following expression is calculated according to the derivative function definition above:
(Derivation '(* x y (+ x 3)' x)
The expected result is y. Unfortunately, this is obviously incorrect. It seems that we have to modify it. First, the power type does not need to be changed. It always only accepts two parameters, so we need to change the addition and multiplication so that they can support multiple parameters, but one thing: I want to reuse the make-sum and make-product functions we have already written. To complete this task, we need to do a simple exercise: Let's write a common addition operation function, let it support multiple parameters (we assume that the "+" number built in scheme can only accept two parameters)
First, we define a function that can only accept two common values as parameters:

; It can accept two common numeric parameters
(Define (make-sum-two x y)
(+ X y ))

Then define an advanced vertex. Although it can only accept two parameters, the second parameter is a list, which reuses the above function:

It can accept a value parameter and a list Parameter
(Define (make-sum-list x list)
(If (null? List)
X
(Make-sum-two x (make-sum-list (car list) (cdr list )))))

With this function, if we want to calculate the join addition of the five numbers 1 to 5, we can write it like this (make-sum-list 1' (2 3 4 5 ))
Haha, although I can do a good job, it is inevitable that such writing is "not decent ". It doesn't matter. Remember that in the SICP2.2 section, exercise question 2.20 mentioned a form called "note-and-tail" (similar to the variable parameters in C) that we can use, so we have the third function:

; It can accept any number of numeric parameters
(Define (make-sum-any x. y)
(Make-sum-list x y ))

Now we can write the following: (make-sum-any 1 2 3 4 5)

We found that, using make-sum-list as a bridge, we can convert make-sum-two that can only accept two parameters into make-sum-any that can accept any parameter, in addition, you do not need to care about the original implementation details of make-sum-two, or modify it. In the same way, in this way, we can update the make-sum and make-product functions in exercise 2.56 to support multiple parameters:

; Constructor and Formula
(Define (make-sum x y)
(Cond (= number? X 0) y)
(= Number? Y 0) x)
(And (number? X) (number? Y) (+ x y ))
(Else (list '+ x y ))))

It can accept a value parameter and a list Parameter
(Define (make-sum-list x list)
(If (null? List)
X
(Make-sum x (make-sum-list (car list) (cdr list )))))

; It can accept any number of numeric parameters
(Define (make-sum-any x. y)
(Make-sum-list x y ))

 

; Constructor
(Define (make-product x y)
(Cond (or (= number? X 0) (= number? Y 0) 0)
(= Number? X 1) y)
(= Number? Y 1) x)
(And (number? X) (number? Y) (* x y ))
(Else (list '* x y ))))

It can accept a value parameter and a list Parameter
(Define (make-product-list x list)
(If (null? List)
X
(Make-product x (make-product-list (car list) (cdr list )))))

; It can accept any number of numeric parameters
(Define (make-product-any x. y)
(Make-product-list x y ))

So far, the work has not been completed, because we found that the selection function for the original number of added and the multiplier is not applicable. For addition, (+ 1 2) and (+ 1 2 3) the number of adders is 1, the former is 2, and the latter is? I think it is (+ 2 3). I have no theoretical basis here, but it works well. In addition, you can also (+ 1 2 3) the result is (+ 1 (+ 2 3). Therefore, the number of adders is 1, and the number of adders is (+ 2 3). The same is true for multiplication, then, we can define the selection function of the number and multiplier as follows:

(Define (augend exp)
(Let (L (length exp )))
(Cond (= L 3) (caddr exp ))
(= L 2) (cadr exp ))
(= L 1) 0)
(Else (cons '+ (cddr exp ))))))

(Define (multiplicand exp)
(Let (L (length exp )))
(Cond (= L 3) (caddr exp ))
(= L 2) (cadr exp ))
(= L 1) 1)
(Else (cons '* (cddr exp ))))))

OK, so far, we can get a complete answer to this exercise:

; Test whether it is a simple variable
(Define (variable? X) (symbol? X ))

To test whether a variable is equal to a certain number.
(Define (= number? X)
(And (number? X) (= x )))

; Test whether two variables are equal
(Define (same-variable x y)
(And (variable? X) (variable? Y) (eq? X y )))

; Construct a power expression
(Define (make-exponentiation x y)
(Cond (or (= number? X 1) (= number? Y 0) 1)
(= Number? Y 1) x)
(= Number? X 0) 0)
(And (number? X) (number? Y ))
(* X (make-exponentiation x (-y 1 ))))
(Else (list '^ x y ))))

; Determine whether it is a power
(Define (exponentiation? Exp)
(And (pair? Exp) (eq? (Car exp) '^ )))

; Evaluate the power base number
(Define (base exp) (cadr exp ))

; Power index
(Define (exponent exp) (caddr exp ))

; Constructor and Formula
(Define (make-sum x y)
(Cond (= number? X 0) y)
(= Number? Y 0) x)
(And (number? X) (number? Y) (+ x y ))
(Else (list '+ x y ))))

It can accept a value parameter and a list Parameter
(Define (make-sum-list x list)
(If (null? List)
X
(Make-sum x (make-sum-list (car list) (cdr list )))))

; It can accept any number of numeric parameters
(Define (make-sum-any x. y)
(Make-sum-list x y ))

; Judge whether the expression is a sum
(Define (sum? X)
(And (pair? X) (eq? (Car x) '+ )))

; Addition in summation
(Define (addend exp) (cadr exp ))

; Number of sheets in the summation form
(Define (augend exp)
(Let (L (length exp )))
(Cond (= L 3) (caddr exp ))
(= L 2) (cadr exp ))
(= L 1) 0)
(Else (cons '+ (cddr exp ))))))

; Constructor
(Define (make-product x y)
(Cond (or (= number? X 0) (= number? Y 0) 0)
(= Number? X 1) y)
(= Number? Y 1) x)
(And (number? X) (number? Y) (* x y ))
(Else (list '* x y ))))

It can accept a value parameter and a list Parameter
(Define (make-product-list x list)
(If (null? List)
X
(Make-product x (make-product-list (car list) (cdr list )))))

; It can accept any number of numeric parameters
(Define (make-product-any x. y)
(Make-product-list x y ))

; Determine whether the expression is a multiplication
(Define (product? X)
(And (pair? X) (eq? (Car x )'*)))

; Calculate the multiplier in the multiplication Formula
(Define (multiplier exp) (cadr exp ))

; Calculate the multiplier in the multiplication Formula
(Define (multiplicand exp)
(Let (L (length exp )))
(Cond (= L 3) (caddr exp ))
(= L 2) (cadr exp ))
(= L 1) 1)
(Else (cons '* (cddr exp ))))))

; Evaluate the derivative (not including the derivation of Division)
(Define (derivation exp var)
(Cond (null? Exp) 0)
(Number? Exp) 0)
(Variable? Exp)
(If (same-variable exp var) 1 0 ))
(Sum? Exp)
(Make-sum-any (derivation (addend exp) var)
(Derivation (augend exp) var )))
(Product? Exp)
(Make-sum-any
(Make-product-any (multiplier exp)
(Derivation (multiplicand exp) var ))
(Make-product-any (multiplicand exp)
(Derivation (multiplier exp) var ))))
(Exponentiation? Exp)
(Let (expt (exponent exp ))
(Bs (base exp )))
(Make-product-any
(Make-product-any expt
(Make-exponentiation bs (-expt 1 )))
(Derivation bs var ))))
(Else
(Begin (display "\ nerror ~ ")
(Display exp)
(Display "and ")
(Display var)
(Display "\ n ")))))

; Test
(Derivation '(+ (^ x 2) (* 2 x y) 'x)
(Derivation '(+ (* x) (* 2 x y) 'x)
(Derivation '(* x y (+ x 3)' x)

 

7. Exercise 2.58

Changed to the infix expression and directly pasted the Code:

(Define (variable? X) (symbol? X ))

(Define (= number? X)
(And (number? X) (= x )))

(Define (same-variable? X y)
(And (variable? X) (variable? Y) (eq? X y )))

(Define (make-sum a1 a2)
(Cond (= number? A1 0) a2)
(= Number? A2 0) a1)
(And (number? A1) (number? A2) (+ a1 a2 ))
(Else (list a1' + a2 ))))

(Define (sum? X)
(And (pair? X) (eq? (Cadr x) '+ )))

(Define (addend exp) (car exp ))
(Define (augend exp) (caddr exp ))

(Define (make-product m 1 m2)
(Cond (or (= number? M1 0) (= number? M2 0) 0)
(= Number? M1 1) m2)
(= Number? M2 1) m1)
(And (number? M1) (number? M2) (* m 1 m2 ))
(Else (list m1 '* m2 ))))

(Define (product? X)
(And (pair? X) (eq? (Cadr x )'*)))

(Define (multiplier exp) (car exp ))

(Define (multiplicand exp) (caddr exp ))

(Define (make-exponentiation base exponent)
(Cond (= number? Exponent 0) 1)
(= Number? Exponent 1) base)
(Else (list base' ** exponent ))))

(Define (exponentiation? X)
(And (pair? X) (eq? (Cadr x )'**)))

(Define (base exponentiation) (car exponentiation ))

(Define (exponent exponentiation) (caddr exponentiation ))

(Define (derivation exp var)
(Cond (number? Exp) 0)
(Variable? Exp)
(If (same-variable? Exp var) 1 0 ))
(Sum? Exp)
(Make-sum (derivation (addend exp) var)
(Derivation (augend exp) var )))
(Product? Exp)
(Make-sum
(Make-product (multiplier exp)
(Derivation (multiplicand exp) var ))
(Make-product (multiplicand exp)
(Derivation (multiplier exp) var ))))
(Exponentiation? Exp)
(Let (expt (exponent exp ))
(Bs (base exp )))
(Make-product
(Make-product expt
(Make-exponentiation bs (-expt 1 )))
(Derivation bs var ))))
(Else
(Error "error ~ "))))

; Test
(Derivation '(x * y) * (x + 3) 'x)

 

Note: This is a reading note, so the content only belongs to the personal understanding, and does not represent the opinions of SiC. As the understanding goes deeper, the content may be modified.

 

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.