Learning notes for publication (1.3.4)

Source: Internet
Author: User

Learning notes for publication (1.3.4)
Zhou yinhui

 

1. process as return value

After we understand the high-order functions in 1.3, "using a process as the return value of another process" is a common thing, such as the followingCode:

(Define (f x)

(+ X 1 ))

(Define (g) F)

(G) 2)

Function G has no parameter, and its return value is function f. So (g) 2) The calculation result is (F 2), and the final calculation result is 3.

A named function is used as the returned result. Accordingly, an "anonymous process" can also be used as the returned result, the "anonymous process" here is our Lambda expression, so the above Code can be transformed:

(Define (g)

(Lambda (x) (+ x 1 )))

(G) 2)

The (g) 2) calculation result is (lambda (x) (+ x 1) 2), and the final calculation result is 3.

 

2. Newton's method

Learn here, you may need to review the basic content of advanced mathematics, including "derivative" and "differential", high online teaching materials can be found here: http://sxyd.sdut.edu.cn/gaoshu1/index.htm

About the introduction of the Newton method can be seen here: http://en.wikipedia.org/wiki/Newton%27s_method, below isProgram:

(Define (close-enough? V1 V2)

(<(ABS (-V1 V2) 0.000000001 ))

; Defines non-moving functions

(Define (fixed-point F first-guess)

(Define (try guess step-count)

(Let (next (F guess )))

(If (close-enough? Guess next)

Next

(Try next (+ step-count 1 )))))

(Try first-guess 0 ))

; Define derivative Functions

(Define (d F)

(Lambda (x dx) (/(-(f (+ x dx) (f x) dx )))

; Newton Method

(Define (Newton g first-guess)

(Fixed-point (lambda (x) (-X (/(g x) (d G) x 0.000000001) First-guess ))

; Square

(Define (square X)

(* X ))

Define the initiator to test the Newton method.

(Define (sq x)

(Newton (lambda (y) (-(square y) X) 1.0 ))

(SQ 5)

 

3. "first-class citizens"

The following lists the "privileges" of the language elements in a program language as "first-class citizens ":

    • You can use the variable name.
    • Can be used as a process parameter
    • Can be returned as a process
    • Can be included in the data structure

4. Practice 1.40

Returns the zero point of x ^ 3 + ax ^ 2 + bx + c.

First, it is proved that f (x) = x ^ 3 + ax ^ 2 + bx + c is "micro:

It is known by the bootable and wealable properties that can be imported and wealable are both necessary and sufficient. Therefore, we can prove that wealable can be passed first,

F' (x) = (x ^ 3) '+ (ax ^ 2)' + (BX) '+ (c )'

= 3x ^ 2 + 2ax + B

So the derivative of f (x) exists, then f (x) can be exported, and it must be micro.

Second, the "Newton method" is used: If f (x) is a microfunction, then a solution of f (x) = 0 is a function (X-f (x) a fixed point of/DF (x), where DF (x) is the derivative of f (x. So we can easily get the following code:

(Define (close-enough? V1 V2)

(<(ABS (-V1 V2) 0.000000001 ))

; Defines non-moving functions

(Define (fixed-point F first-guess)

(Define (try guess step-count)

(Let (next (F guess )))

(If (close-enough? Guess next)

Next

(Try next (+ step-count 1 )))))

(Try first-guess 0 ))

; Define derivative Functions

(Define (d F)

(Lambda (x dx) (/(-(f (+ x dx) (f x) dx )))

; Newton Method

(Define (Newton g first-guess)

(Fixed-point (lambda (x) (-X (/(g x) (d G) x 0.000000001) First-guess ))

Define the cubic function, which is the so-called F (x) in our question)

(Define (cubic a B C)

(Lambda (x) (+ (* x) (* a x) (* B x) C )))

; Just define several coefficients

(Define a 3)

(Define B 5)

(Define c 8)

(Define result (Newton (cubic a B C) 1.0 ))

Define a verification process, let it verify the solution, whether to let the equation be true

(Define (validate X)

(= 0 (+ (* x) (* a x) (* B x) C )))

; Output result

Result

; Verification Result

(Validate result)

For example, if we calculate x ^ 3 + 3x ^ 2 + 5x + 8 = 0, the solution is-2.3282688556686084.

5. Practice 1.41

Double is calculated twice by function f, then (double) is calculated four times, (double) is calculated 14 times, so the return value should be 21, run the following code:

(Define (double F)

(Lambda (x) (f (f x ))))

(Define (inc a) (+ A 1 ))

(Double) INC) 5)

6. Practice 1.42

The composite function is very simple, and the answer is directly posted:

(Define (inc a) (+ A 1 ))

(Define (Square A) (* ))

(Define (compose f g)

(Lambda (x) (f (g x ))))

(Compose square Inc) 6)

 

7. Exercise 1.43

F (f (F... (F (x )...)) Actually, function f is compound N times:

(Define (inc a) (+ A 1 ))

(Define (Square A) (* ))

(Define (compose f g)

(Lambda (x) (f (g x ))))

(Define (repeated F count)

(Define (repeated-inner F step)

If the compound times are specified, the function after the compound is returned.

(If (= step count)

F

Otherwise, it will be composite again

(Repeated-inner (compose f) (+ step 1 ))))

(Lambda (x) (repeated-inner F 1) X )))

(Repeated square 2) 5)

 

8. Practice 1.44

; Define composite Functions

(Define (compose f g)

(Lambda (x) (f (g x ))))

; Define n composite Functions

(Define (repeated F count)

(Define (repeated-inner F step)

(If (= step count)

F

(Repeated-inner (compose f) (+ step 1 ))))

(Lambda (x) (repeated-inner F 1) X )))

; Define a smoothing function

(Define (smooth F)

(Lambda (x )(/

(+ (F (-x 0.0001) (f x) (f (+ x 0.0001 )))

3 )))

Define n smoothing functions

(Define (smooth-n f count)

(Repeated (smooth f) count ))

 

9. Practice 1.45

Basically all the previous work results are used: Fixed Point, average damping, and composite functions.

(Define (close-enough? V1 V2)

(<(ABS (-V1 V2) 1.0e-20 ))

; Defines non-moving functions

(Define (fixed-point F first-guess)

(Define (try guess step-count)

(Let (next (F guess )))

(If (close-enough? Guess next)

Next

(Try next (+ step-count 1 )))))

(Try first-guess 0 ))

; Define composite Functions

(Define (compose f g)

(Lambda (x) (f (g x ))))

; Define n composite Functions

(Define (repeated F count)

(Define (repeated-inner F step)

(If (= step count)

F

(Repeated-inner (compose f) (+ step 1 ))))

(Lambda (x) (repeated-inner F 1) X )))

Define the Npower of X

(Define (Power x n)

(Define (power-inner step result)

(If (= Step N)

Result

(Power-inner (+ step 1) (* result X ))))

(Power-inner 0 1 ))

; Define the average damping function

(Define (average V1 V2)

(/(+ V1 V2) 2.0 ))

; Average damping of the currying version

(Define (avg V1)

(Lambda (V2) (/(+ V1 V2) 2.0 )))

Define n average damping Functions

(Define (average-N V1 V2 N)

(Repeated (avg V1) n) V2 ))

; Defines the square root

(Define (root X)

(Fixed-point (lambda (y) (average y (/x y) 1.0 ))

; Defines the n Root

(Define (root-N x n)

(Fixed-point (lambda (y) (average-n y (/X (Power Y (-N 1) n) 1.0 ))

Test, calculate the square root of 2

(Root 2)

Test, calculate the 3rd power of 12

(Root-N 12 3)

It is worth noting that N average damping functions are defined. Because we define (repeated F count), the default function f contains only one parameter, therefore, the average function with two parameters cannot be used for composite operations. Therefore, we need to convert currying into a parameter, that is, our AVG version. In addition, when average-N is used, I set its average damping times to n (that is, the number of times of compaction). As for how many times can be converged, I did not try it.

 

10. Practice 1.46

Simply translate the meaning of the question into code by means of a tail recursion:

(Define (interative-improve isgood improveit)

(Lambda (X)

; Return value if the value is good enough

(If (isgood X)

X

Otherwise, the corrected value (improveit X) is taken for calculation.

(Interative-improve isgood improveit) (improveit X )))))

The following functions are test functions:

(Define (good-enough? X)

(<X 1.0e-10 ))

(Define (improve X)

(* X 0.1 ))

(Interative-improve good-enough? Improve) 5.0)

 

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.