Study Notes and question solutions for SiCp-constructor process abstraction (1)

Source: Internet
Author: User

I haven't read this book for a while.

In addition, I had some questions when taking notes, and I thought it would be meaningless to take notes based on books. so, I changed my approach. change to raise a question first, write down the key points, learn relevant things based on the actual case, and finally attach the question,

OK. The following is the first note (still the old routine)

Content of this section

  • L discuss basic scheme syntax rules
  • L Process Definition
  • L substitution model
  • L conditional expressions and predicates
  • L process Abstraction
  • L comparison with C

Basic elements of Programming

All advanced languages are unique in the combination of simple cognition to form a complex cognitive method, and each powerful language provides three mechanisms for this:

  • L basic expression: used to represent the simplest individual concerned with the language
  • L combination method: construct complex elements from simple things
  • L abstract method: Name composite objects and operate them as units.

In programming, we need to process two types of elements: data and process.

Basic Scheme syntax rules

It is an interactive language, similar to the python interpreter running method. When its interpreter is running, it repeatedly executes a "Read-evaluate-print" loop process, that is, recursive calls are very common in scheme. each cycle:

  • ? Read a complete expression
  • ? Evaluate the expression of a pair
  • ? Returns the obtained value.

Compared with C, C is a compilation language that requires a complete structure of the program. Expressions and statements are not programs and cannot be compiled and run, in addition, compiled programs must be compiled and run. In terms of structure, the C language also has expressions describing basic operations and statements describing basic actions, there are various combinations of statements (control flow), and its functional language abstraction mechanism.

The C language also strictly distinguishes data from the data operation process (Code). In scheme, data and processes can be converted naturally: data can be used as the code to be executed, code can be processed as data.


Demonstrate the basic syntax of scheme:

1. Expression


2. Evaluate the compound expression:

The sum of values is usually combined. The Interpreter works in the following way:

  1. Evaluate each of the combined subexpressions
  2. Apply the value of the leftmost subexpression (operator value, which should be a process) to the corresponding actual parameter (value obtained by other subexpressions)

Example: (* (+ 2 (* 4 6) (+ 3 5 7 ))

The combined evaluation requires that the subexpression be evaluated first. Therefore, the value process is recursive.The evaluate process can be represented by a tree,First obtain the value of the terminal (leaf) node and then accumulate,In the end, the value tree of the entire expression is generated at the root of the tree with a recursive structure. Recursive processing is natural.


3. Variables


4. Composite Process


The basic process definition form is:

Square process definition:

(Define (<Name> <formalparameters>) (body ))

What do process name-form parameters do (how to evaluate)

Replacement Model

Predefined basic processes (Operations) and special forms are the basic components of the Construction Program. This component set can be expanded by defining the process as needed, we define a process to abstract the computing details of this process. You only need to use this process like using an operator. In the preceding example, it cannot be seen from the usage that square is a basic operation or a user-defined process.

The use and power of a composite process is the same as that of basic operations. process definition is one of the most important techniques for decomposing and controlling program complexity.

The value definition expression associates the corresponding computing process with the name. The calculation process determined by the combined and composite processes is (substitution model):

A) obtain the value of each parameter expression (subexpression ).

B) locate the definition of the process to be called (based on the evaluate result of the first subexpression)

C) use the form parameters in the calculated actual parameter substitution process body

D) evaluate the process body

The substitution model provides a semantics for process definition and process application. Many Scheme process behaviors can be described using this model. Later we will see that an extended semantic model is required for more complex processes.

Note:

  • ? The substitution model is only used to help you intuitively understand the behavior of process applications.
  • ? It does not reflect the actual working process of the interpreter.
  • ? The actual interpreter is discussed later. It is implemented based on the environment.
  • ? The substitution model is the simplest and easy to understand, but it is not enough to explain all the actual programs.
  • ? Its limitation is that it cannot explain programs with variable data.
  • ? More detailed models will be introduced later

Application order and regular order

For compound process operations, the interpreter first evaluates the subexpression (operator and each operation object), and then applies the obtained operation to the operation object (actual parameter ). this approach is reasonable, but not unique. the other method is to calculate the object without asking for a value, and then calculate the value when necessary.

The previous method (first evaluate parameter and then apply operator) is called application order evaluation, and the next method (completely expanded and then reduced) is called regular order evaluation. Scheme uses Application sequencing to avoid repeated computations. Other functions will be discussed later.

Scheme conditional expressions and predicates

Scheme has a conditional expression. The absolute value function can be defined:

The general form of the conditional expression:

 

The absolute value function can also be defined:


Else indicates the conditions that are always true and should be placed at the end.

Simplified conditional expressions:

(If <predicate> <consequent> <alternative>)

Cond and if are both special forms and have special evaluate rules.

Logical combination operators and or are also special forms, using special evaluate methods

(And <E1>... <en>)

Evaluate e one by one until an E is obtained as false or the last E is evaluated. Take the value of the subexpression that is finally evaluated as the value

(Or <E1>... <en>)

Evaluate e one by one until an E returns the truth or the final E returns the result. Take the value of the subexpression that is finally evaluated as the value

(Not <E>) if the value of E is not true, it must be true. Otherwise, it must be false.

A predicate refers to the process of returning true or false results, or the expressions that can be used to obtain true or false results.Relational operators are basic predicates. They can use and, or, and not to combine various complex logical conditions and define predicates using procedures.

Question

Practice 1.1

Directly input the expression into the compiler to get the result.

Practice 1.2

Change the infix expression to a prefix expression:

(/(+ 5 4 (-2 (-3 (+ 6 (/4 5 )))))

(* 3 (-6 2) (-2 7 )))

Practice 1.3

My solution to this question is: Find the minimum of the three numbers, and then subtract the sum of the three numbers.

First, find the maximum code of the three numbers:

(define (biggest x y z)    (define (bigger m n)      (if (< n m)          m          n))    ( bigger x (bigger y z)))
Then the question answer:

(define (sum-of-bigger x y z)    (define (smallest x y z)    (define (smaller m n)      (if (< n m)          n          m))      (smaller x (smaller y z)))    (- (+ x y z) (smallest x y z)))

The result is as follows:

Practice 1.4

This solution is not available.

Practice 1.5

The key to answering this question is to understand the difference between the application order and the regular order (the 1.1.5 section of the book has a detailed description ).

First, it can be determined that no matter what evaluate method the interpreter uses(P)Always enters an infinite loop because the functionPWill constantly call itself:

(define (p) (p))

In the interpreter, execute(P)The call will hold the interpreter stuck, and the interpreter process can only be forced to be killed:

1] => (p) ^ Z [1] + stopped Mit-scheme $ killall Mit-Scheme

In the application sequence, all input parameters are evaluated immediately. Therefore, execute(Test 0 (p ))Actual Parameters0And(P)Will be evaluated, and(P)The interpreter enters an infinite loop. Therefore, if an interpreter stops running Ben's test, the interpreter uses the application order evaluation mode.

On the other hand, in the regular order, the actual parameters passed in will be evaluated only when necessary. Therefore, run in the interpreter using the regular order(Test 0 (p )),0And(P)Will not be evaluated immediately.IfFormat parametersX(That is0) Will be evaluated (the result is also0), And then0For comparison ((= X 0)), Because the Compare value is true (# T), SoIfReturn0As the value of the value expression, and this value isTestThe function value is returned.

Because in the value of the regular order(P)No execution is performed from the beginning to the end, so there will be no infinite loop. Therefore, if an interpreter returns smoothly when running Ben's test0The interpreter uses the regular order value mode.

Note

Another thing to note is the terms "formal parameters" and "actual parameters.

For a function, the local name of the parameter it accepts is called a formal parameter.

The expression passed in when a function is called an actual parameter.

For example(Define (square X) (* X ))For example,XIs the form parameter.(Square 2),2Is the form parameterX.

When people only say "parameter" without specifying whether it is "form parameter" or "actual parameter", they generally refer to "form parameter 』, but it depends on the context.


Study Notes and question solutions for SiCp-constructor process abstraction (1)

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.