Scheme language summary (on)

zhaozj2021-02-17  48

As a LISP variant, Scheme is a very simple computing language that uses its programmer to get rid of the complexity of the language itself, focusing on a more important issue, making language truly a tool for solving problems. This article is divided into top and lower parts to introduce the Scheme language.

One. Scheme language features

The Scheme language is a dialect (or a variable species), which was born in 1975, for this programming language for nearly 30 years, it did not be like C , Java, and C # The favor, it is more distinctive in China. However, it has a wide application in the field of computer education abroad, and many people's first computer language is the Scheme language.

It is a small and powerful language as a multi-purpose programming language, which can be used as a scripting language, or it can be used as an extension language of the application software. It has meta-language characteristics, and there are many unique features. It is called the "Queen" in the programming language.

Below is the summary of the programming features of the Scheme language:

Lexical Scoping Dynamic Typing Dynamic Type Tail Recrete Function As the value returns to support first-class computing continuous transmission (PASSING-BY-VALUE) arithmetic operation is relatively independent

two. Standard and Implementation of Scheme Language

R5RS (REVISED (5) Report On The Algorithmic Language Scheme

The 5th revision of the syntax rules of the Scheme language, in 1998, the current standard of the Scheme language, currently the implementation of most Scheme language will meet or follow this standard, and almost all have some of its own extension features.

Guile (GNU's Extension Language)

Guile is a project of the GNU project. It is a GNU extended language library. It is also a concrete implementation of the Scheme language; if you pack it as a library, you can link it to your application and make your app Has your own scripting language, this scripting language is currently the Scheme language.

Guile can run on Linux and some UNIX systems, below is a simple installation process:

Download Guile-1.6.4, file named Guile-1.6.4.tar.gz, execute the following command: TAR XVFZ Guile-1.6.4.tar.gz

CD Guile-1.6.4

./configure

Make

Make Install

So, you can execute the command guile, enter the Guile> prompt state, and enter the debug scheme program code, all the code in this article is debugged at Guile.

Other implementation

In addition to Guile, there are many implementations of the Scheme language, such as: GNU / Mit-Scheme, SCI, Scheme48, Drscheme, etc. Most of them are open source, freely download and installation, and there are many cross-platform implementations. You will find both the Scheme language interpreter of Basic, and compile the Scheme language into a C language, and compile the Scheme language code into a compiler with the SCHEME language code as a compiler like Java.

three. basic concepts

Comment

The comments in the Scheme language are single-line comments, and start until the end of the semicolon [;], the content is not commented, and the program is not handled when the program is running, such as:; this is a scheme comment line.

There is no more annotations in the standard Scheme language definition, but almost all in its implementation. There are multi-line comments in Guile, starting with the symbol combination "#!", With the opposite another symbol combination "! #" End, where is the annotation, such as:

#!

There is scheme.

You can write mility lines here.

! #

Note that the symbol combination "#!" And "! #" Must be written in two lines.

Scheme uses a scripting language

The Scheme language can be used as a scripting language as a scripting language like SH, Perl, Python, using it to write an executable script, if you write an executable script with the Scheme language via Guile, its first line and the second The line is generally similar to the following:

#! / usr / local / bin / guile -s

! #

Such a code is automatically called to interpret the execution, and the standard file diaphragm is ".scm".

Block (FORM)

Block (FORM) is the minimum program unit in the Scheme language, and a Scheme language program consists of one or more form. FORM is enclosed by small brackets without special instructions, such as:

(Define x 123)

( 1 2)

(* 4 5 6)

(Display "Hello World")

A Form can also be an expression, a variable definition, or a process.

FORM nested

The SCHEME language allows Form's nested, which makes it easy to implement complex expressions and is also a very distinctive expression. The following figure shows the operation of the nesting slightly complicated expression:

Variable definitions

You can define a variable with define, as follows: (define variable name)

As: (Define X 123), define a variable x whose value is 123.

Change the value of the variable

You can use set! To change the value of the variable, the format is as follows: (set! Variable name)

Such as: (set! X "Hello"), change the value of the variable x to "Hello".

The Scheme language is a high-level language, like many advanced languages ​​(such as Python, Perl), which can be changed at any time.

four. type of data

1. Simple data type

Logical (Boolean)

The most basic data type, is also the simplest data type supported by many computer languages, only two values: #T, equivalent to true; #f, equivalent to FALSE in other computer languages.

The Boolean type in the Scheme language is only one action: NOT. It is intended to take the opposite value, namely:

(not #f) => #t

(not #t) => #f

NOT reference, similar to logic non-computational operation

Guile> (not 1)

#f

Guile> (Not (List 1 2 3)))

#f

Guile> (not 'a)

#f

It can be seen from the above operation, as long as the parameters behind the NOT are not logical, their return value is #F. Digital (NUMBER)

It is divided into four types: Integer, Rational, Real, Complex, is also known as digital type (Number).

Such as: complex type (COMPLEX) can be defined as (REAL) can be defined as (Define F 22/7) Agestal number can be defined as (DEFINE P 3.1415) integer type (Integer) Can be defined as (Define I 123)

In the Scheme language, digital types of data can also be classified, namely binary, octal, decimal, and hexadecimal, in the form of symbol combination #b, #o, #d, and #x as a representation. The prefix of the digital administration, which indicates that the decimal #d can omit, such as: binary # b1010, octal # o567, decimal 123 or # d123, hexadecimal # x1afc.

This strict accordance with the Mathematical Theorem can be used to classify the digital type. It can be seen that the Scheme language penetrates deep mathematics idea, and the Scheme language is created by mathematicians. It is more distinct in this regard. .

Character type (char)

The character data in the Scheme language begins with a symbol combination "# /", indicating a single character, can be letters, numbers or "[! $% & * -. /:% Lt; =>? @ ^ _ ~] "Wait other characters, such as: # / a indicates uppercase letters A, # / 0 represents character 0, where special characters are: # / space indicates that space characters and # / newline represents a wrap.

Symbol

The symbol type is a symbolic name in the Scheme language, which can be a word, a plurality of words enclosed in parentheses, or a meaningful letter combination or symbol combination, it can be understood in a sense to c The enumeration type. Look at the following:

Guile> (Quote XYZ)); Defining Variable A is symbol type, value is XYZ

Guile> a

xyz

Guile> (Define XYZ 'a); Defining Variable XYZ is symbol type, value is a

Guile> XYZ

a

The single quotes' and quote are equivalent, and more simple. The symbol type is different from the string that the symbol type cannot be used as a string, and the value of a member character can be obtained, but the two can be converted between each other.

content:

One. Characteristics of Scheme Language. Standard and Implementation of Scheme Language. Basic concept. Data type five. Process definition about the author

related information:

Scheme language summary (below)

In the Linux area:

Tutorial Tools & Product Codes & Component Articles

Song Guowei (GWSONG52@sohu.com) December 2003

As a LISP variant, Scheme is a very simple computing language that uses its programmer to get rid of the complexity of the language itself, focusing on a more important issue, making language truly a tool for solving problems. This article is divided into top and lower parts to introduce the Scheme language.

One. Scheme language features

The Scheme language is a dialect (or a variable species), which was born in 1975, for this programming language for nearly 30 years, it did not be like C , Java, and C # The favor, it is more distinctive in China. However, it has a wide application in the field of computer education abroad, and many people's first computer language is the Scheme language. It is a small and powerful language as a multi-purpose programming language, which can be used as a scripting language, or it can be used as an extension language of the application software. It has meta-language characteristics, and there are many unique features. It is called the "Queen" in the programming language.

Below is the summary of the programming features of the Scheme language:

Lexical Scoping Dynamic Typing Dynamic Type Tail Recrete Function As the value returns to support first-class computing continuous transmission (PASSING-BY-VALUE) arithmetic operation is relatively independent

The purpose of this article is to make a programming base (even a little bit) friend can master the syntax rules of the Scheme language as soon as possible. If you are reading this article, it is found that he has used Scheme language, then my purpose is reached. .

two. Standard and Implementation of Scheme Language

R5RS (REVISED (5) Report On The Algorithmic Language Scheme

The 5th revision of the syntax rules of the Scheme language, in 1998, the current standard of the Scheme language, currently the implementation of most Scheme language will meet or follow this standard, and almost all have some of its own extension features.

Guile (GNU's Extension Language)

Guile is a project of the GNU project. It is a GNU extended language library. It is also a concrete implementation of the Scheme language; if you pack it as a library, you can link it to your application and make your app Has your own scripting language, this scripting language is currently the Scheme language.

Guile can run on Linux and some UNIX systems, below is a simple installation process:

Download Guile-1.6.4, file named Guile-1.6.4.tar.gz, execute the following command:

TAR XVFZ Guile-1.6.4.tar.gz

CD Guile-1.6.4

./configure

Make

Make Install

So, you can execute the command guile, enter the Guile> prompt state, and enter the debug scheme program code, all the code in this article is debugged at Guile.

Other implementation

In addition to Guile, there are many implementations of the Scheme language, such as: GNU / Mit-Scheme, SCI, Scheme48, Drscheme, etc. Most of them are open source, freely download and installation, and there are many cross-platform implementations. You will find both the Scheme language interpreter of Basic, and compile the Scheme language into a C language, and compile the Scheme language code into a compiler with the SCHEME language code as a compiler like Java.

three. basic concepts

Comment

The comments in the Scheme language are single-line annotations, start until the end of the line, and the content is comment, which is not handled when the program is running, such as:

This is a scheme comment line.

There is no more annotations in the standard Scheme language definition, but almost all in its implementation. There are multi-line comments in Guile, starting with the symbol combination "#!", With the opposite another symbol combination "! #" End, where is the annotation, such as:

#!

There is scheme.

You can write mility lines here.

! #

Note that the symbol combination "#!" And "! #" Must be written in two lines.

Scheme uses a scripting language

The Scheme language can be used as a scripting language as a scripting language like SH, Perl, Python, using it to write an executable script, if you write an executable script with the Scheme language via Guile, its first line and the second The line is generally similar to the following:

#! / usr / local / bin / guile -s

! #

Such a code is automatically called to interpret the execution, and the standard file diaphragm is ".scm".

Block (FORM)

Block (FORM) is the minimum program unit in the Scheme language, and a Scheme language program consists of one or more form. FORM is enclosed by small brackets without special instructions, such as:

(Define x 123)

( 1 2)

(* 4 5 6)

(Display "Hello World")

A Form can also be an expression, a variable definition, or a process.

FORM nested

The SCHEME language allows Form's nested, which makes it easy to implement complex expressions and is also a very distinctive expression. The following figure shows the operation of the nesting slightly complicated expression:

Variable definitions

You can define a variable with define, as follows: (define variable name)

As: (Define X 123), define a variable x whose value is 123.

Change the value of the variable

You can use set! To change the value of the variable, the format is as follows: (set! Variable name)

Such as: (set! X "Hello"), change the value of the variable x to "Hello".

The Scheme language is a high-level language, like many advanced languages ​​(such as Python, Perl), which can be changed at any time.

four. type of data

1. Simple data type

Logical (Boolean)

The most basic data type, is also the simplest data type supported by many computer languages, only two values: #T, equivalent to true; #f, equivalent to FALSE in other computer languages.

The Boolean type in the Scheme language is only one action: NOT. It is intended to take the opposite value, namely:

(not #f) => #t

(not #t) => #f

NOT reference, similar to logic non-computational operation

Guile> (not 1)

#f

Guile> (Not (List 1 2 3)))

#f

Guile> (not 'a)

#f

It can be seen from the above operation, as long as the parameters behind the NOT are not logical, their return value is #F.

Digital (NUMBER)

It is divided into four types: Integer, Rational, Real, Complex, is also known as digital type (Number). Such as: complex type (COMPLEX) can be defined as (REAL) can be defined as (Define F 22/7) Agestal number can be defined as (DEFINE P 3.1415) integer type (Integer) Can be defined as (Define I 123)

In the Scheme language, digital types of data can also be classified, namely binary, octal, decimal, and hexadecimal, in the form of symbol combination #b, #o, #d, and #x as a representation. The prefix of the digital administration, which indicates that the decimal #d can omit, such as: binary # b1010, octal # o567, decimal 123 or # d123, hexadecimal # x1afc.

This strict accordance with the Mathematical Theorem can be used to classify the digital type. It can be seen that the Scheme language penetrates deep mathematics idea, and the Scheme language is created by mathematicians. It is more distinct in this regard. .

Character type (char)

The character data in the Scheme language begins with a symbol combination "# /", indicating a single character, can be letters, numbers or "[! $% & * -. /:% Lt; =>? @ ^ _ ~] "Wait other characters, such as: # / a indicates uppercase letters A, # / 0 represents character 0, where special characters are: # / space indicates that space characters and # / newline represents a wrap.

Symbol

The symbol type is a symbolic name in the Scheme language, which can be a word, a plurality of words enclosed in parentheses, or a meaningful letter combination or symbol combination, it can be understood in a sense to c The enumeration type. Look at the following:

Guile> (Quote XYZ)); Defining Variable A is symbol type, value is XYZ

Guile> a

xyz

Guile> (Define XYZ 'a); Defining Variable XYZ is symbol type, value is a

Guile> XYZ

a

The single quotes' and quote are equivalent, and more simple. The symbol type is different from the string that the symbol type cannot be used as a string, and the value of a member character can be obtained, but the two can be converted between each other.

2. Composite data type

It can be said that the composite data type is a data type formed by the basic simple data type by combining the data. It is characterized by receiving multiple or more single simple data type data, mostly based on a mathematical model created. .

String (String) The data type consisting of multiple characters can be written directly into content from the double quotes, such as "Hello". Below is the string definition and related operations in Guile:

Guile> (Define Name "Tomson")

Guile> Name

"Tomson"

Guile> (String-length name); the length of string

6

Guile> (String-set! name 0 # / g); change the first letter (0 characters) for lowercase letters G (# / g)

Guile> Name

"gomson"

Guile> (String-Ref Name 3); get the third character of the left side of the string (start from 0) # / s

Strings can also be defined in the following form:

Guile> (define # / h # / e # / l # / l # / o))))

Guile> Other

"hello"

Instead of quotation marks when quotation marks appear in strings, such as "ABC /" DEF ".

Point pair (PAIR)

I translated it into "point pair", it is a very interesting type, and some other types of fundamental types, which consists of two meanings separated by it. For example: (1. 2) or (a. B), note that there is a space in both sides.

This is the simplest composite data type, which is also the basic type of other composite data types, such as the list type (list) is implemented by it.

According to the convention in the Scheme language description, the following we use the symbol combination "=>" to represent the value of the expression.

It defines this with cons, such as: (CONS 8 9) => (8. 9)

The value in front of the point is called Car, and the value behind the point is called the CDR, CAR and CDR, and the process of taking the two values ​​of Pair, such as:

(Define P (cons 4 5)) => (4. 5)

(car P) => 4

(CDR P) => 5

You can also set these two values ​​separately with SET-Car! And SET-CDR!

(set-car! p "hello")

(Set-CDR! P "good")

As such, the previous defined P has become ("Hello". "Good").

List (list)

The list is a data type consisting of multiple identical or different data, which is one of the most commonly used composite data types in the program, and many process operations are related to it. Below is the definition and related operations of the list in Guile:

Guile> (Define La (List 1 2 3 4))))

Guile> LA

(1 2 3 4)

Guile> (Length LA); the length of the list

4

Guile> (List-Ref La 3); get the value of the list 3 item (starting from 0)

4

Guile> (List-set! la 2 99); Settings list 2 of 99

99

Guile> LA

(1 2 99 4)

Guile> (Define Y); Create a list

Guile> Y

(6 6 6 6 6)

Make-list is used to create a list, the first parameter is the length of the list, the second parameter is the content added in the list; you can also implement multiple lists, that is, the elements of the list are also lists, such as: (List 1 2) 3) (List 4 5 6)).

List relationship with pair

Tall back, let's take a look at the definition below:

Guile> (Define A (cons 2 (cons 3 ')))))))))))

Guile> a

(1 2 3)

As can be seen, A is originally the point pair we defined above, and finally the result is a list. The fact that the list is a special format formed on the basis of point pairs.

Look at the following code:

Guile> (DEFINE LS (List 1 2 3 4)))

Guile> LS

(1 2 3 4)

Guile> (List? ls)

#T #T #T

Guile> (Pair? ls)

#T #T #T

It can be seen that List is a subtype of Pair, and List must be a pair, while Pair is not List.

Guile> (CAR LS)

1

Guile> (CDR LS)

(2 3 4)

Its CDR is also a list, which can be seen that the operation of the PAIR can be used in List.

Guile> (CADR LS); this "point to" object's CAR

2

Guile> (CDDR LS); this "point to" object's CDR CDR

(3 4)

Guile> (CADDR LS); this "point to" object's CDR's CAR

3

Guile> (CDDDR LS); this "point to" object's CDR's CDR CDR

(4)

The CADR, CDDR, etc. used in operation is a process of data operations that are specifically formed on PAIR data reconstruction, up to four digits A or D, such as CDDDR, CaAddr, etc.

The following figure shows a list of defined by pairs:

This list can be defined by PaIR as the following form:

(Define X (cons 'A (cons' C (CONS 'D' ()))))))))))

The actual content of the list is: (a b C d)

It can also be seen from the PAIR type to easily represent the tree structure, especially the standard binary tree.

Vector

It can be said to be a very easy-to-use type. It is an object that is indexed by an integer, a heterologous data structure, which is less than the list of the same elements, in appearance:

The column is shown as: (1 2 3 4) Vector is: # (1 2 3 4) can be defined normally: (DEFINE V (Vector 3 4 5)) can also be directly defined: (Define V # (3 4 5))

Vector is a relatively commonly used composite type, its element index starts from 0, and this is a bit similar to the array in C language.

Commonly used operation procedure for the vector table (VECTOR):

Guile> (DEFINE V (Vector 1 2 3 4 5)))

Guile> V

# (1 2 3 4 5)

Guile> (Vector-REF V 0); seeking the value of the nth variable

1

Guile> (Vector-Length V); seeking the length of the Vector

5

Guile> (Vector-set! V 2 "ABC"); set the value of the vector nth element

Guile> V

# (1 2 "ABC" 4 5)

Guile> (Make X (Make-Vector 5 6); create vector table

Guile> x

# (6 6 6 6 6)

Make-Vector is used to create a vector table, the first parameter is quantity, the latter parameter is the value added, which is very similar to the Make-list in the list.

We can see that in the Scheme language, each data type has some basic and related operations, such as strings, lists, etc. related to operations, these operational processes are very regular, process names between words It is easy to understand. For friends who have learned C , the method of a certain object is only different in the form of performance. 3. Type judgment, comparison, operation, conversion and method

Type judgment

All judgments in the Scheme language are composed of corresponding constants or variables with the type name, and the shape is:

(Type? Variable)

The Scheme language has a strict definition in the type definition, as in some languages ​​such as C language, in place of the logical type data FALSE, is not allowed in the Scheme language.

The following is a common type judgment and additional instructions:

Logical:

(Boolean? #t) => #t

(Boolean? #f) => #t because #t and #f are both boolean type, so its value is #t

(Boolean? 2) => #f because 2 is a digital type, so its value is #F

Character pattern

(char? # / space) => #t

(char? # / newline) => # Two special characters above: spaces and wraps

(CHAR? # / f) => #t lowercase letters f

(CHAR? # /;) => #t semicolon;

(CHAR? # / 5) => #T character 5, these are correct, so the return value is #t

(CHAR? 5) => #f This is a number 5, not a character type, so return #f

Digital type

(Integer? 1) => #t

(Integer? 2345) => #t

(Integer? -90) => #T above three numbers are integers

(Integer? 8.9) => #f 8.9 inextens

(Rational? 22/7) => #t

(Rational? 2.3) => #t

(REAL? 1.2) => #t

(REAL? 3.14159) => #t

(REAL? -198.34) => #T The above three numbers are real types

(REAL? 23) => #t Because the integer belongs to the real shape

(NUMBER? 5) => #t

(Number? 2.345) => #t

(Number? 22/7) => #t

Other type

(NULL? '()) => # n means an empty type, it is represented as' (), 即 Nothing in parentheses

(NULL? 5) => #f

(Define x 123) Defining variable x its value is 123

(Symbol? x) => #f

(Symbol? 'x) => #t; at this time,' X is symbol X, does not represent the value of the variable x

Such a large type of type judgment in the Scheme language makes the Scheme language have a very good feature. That is, whether the parameter of the judgment process is required.

Compare operation

The relationship of the digital type value or expression can be used in the Scheme language. If the determination variable x is equal to zero, its form is like this: (= x 0), such as X The value is 0, the value of the expression is #t, otherwise #f. There is also the following operations:

(EQV? 34 34) => #t

(= 34 34) => #t

The above two FORM functions are the same, and EQV is described in EQV? Or the number of numbers can also be used.

There are three equal definitions in the Scheme language, and the two variables are exactly the same object; the two objects have the same value; the two objects have the same structure and the content in the structure is the same. In addition to the symbol judgment process and EQV mentioned above, EQ? And Equal? ​​Also the process of judging if it is equal.

EQ?, EQV?, equal?

EQ?, EQV? and Equal? ​​is three processes that judge whether the two parameters is equal, where EQ? and EQV? Features are basically the same, only in different scheme languages.

EQ? It is determined whether the two parameters point to the same object, if it returns #t; equal? ​​is judge whether the two objects have the same structure and the content in the structure is the same, it uses EQ? to compare the members Number; Equal? ​​Multi-purpose composite structure data types such as point pairs, lists, vector tables, strings, etc..

Guile> (DEFINE V (Vector 3 4 5)))

Guile> (Define W # (3 4 5)); W and V are all VECTOR types, have the same value # (3 4 5)

Guile> (EQ? V W)

#f; at this time, W and V are two objects

Guile> (Equal? ​​v W)

#T; judgment requirements in accordance with Equal?

The above operations illustrate the differences between EQ? And Equal? ​​The following operations have proved this:

Guile> (Define X (Make-Vector 5 6))

Guile> x

# (6 6 6 6 6)

Guile> (EQ? X); is the same object, so return #T

#T #T #T

Guile> (Define Z (Make-Vector 5 6))

Guile> Z

# (6 6 6 6 6)

Guile> (EQ? X Z); not the same object

#f

Guile> (equal? ​​x z); the structure is the same, the content is the same, so return #t

#T #T #T

Arithmetic operation

The operators in the Scheme language include: , -, *, / and expond (index operation) where - and / can also be used for single operations, such as:

(- 4) => -4

(/ 4) => 1/4

In addition, there are many extension libraries to provide a lot of useful processes.

MAX is the largest (Max 8 89 90 213) => 213

Min seeks minimum (MIN 3 4 5 6 7) => 3

ABS seeks absolute value (ABS-7) ==> 7

In addition to MAX, Min, ABS, there are many mathematical operations, which is related to the operating environment you use, but they are mostly the same. Many computational processes are specified in R5RS, which can be easily found in R5RS reference.

Conversion

The SCHEME language uses symbol combination "->" to indicate the process of transition between types (very like C language), like a question mark to indicate the type judgment process. Here are some common type conversion processes: Guile> (Number-> String 123); digital conversion is a string

"123"

Guile> (String-> Number "456); string conversion to numbers

456

Guile> (Char-> Integer # / a); Character Convert to Integer, the ASCII code value of lowercase letters A is 96

97

Guile> (char-> integer # / a); uppercase letter A value is 65

65

Guile> (Integer-> Char 97); integer converted to characters

# / a

Guile> (String-> List "Hello"); String Convert to List

(# / h # / e # / l # / l # / o)

Guile> (List-> string (make-list 4 # / a)); list conversion to string

"aaaa"

Guile> (String-> Symbol "Good"); String Convert to Symbol Type

good

Guile> (Symbol-> string 'better; symbol type converted to string

"better"

Fives. Process definition

Procedure

In the Scheme language, the process is equivalent to a function in the C language, and the ScherMe language process is a data type, which is why the Scheme language will process the program and data as the same object. If we enter the plus sign at the Guile prompt and then enter the car, the following situation will appear:

Guile>

#

This tells us " " is a process, and it is an original process, the most basic process in the Scheme language, which is implemented inside the Guile, which is the same as the type, such as Boolean?, Etc., they are Scheme The most basic definition in the language. Note: Different Scheme language implementation environments, the prompt information that can be diligent, but the meaning is the same.

Define can not only define variables, but also define a process because the process (or function) is a data type in the Scheme language, so it can be defined by define. Different is the standard process definition to use Lambda keywords to identify.

Lambda keyword

The process of lambda can be defined in the scheme language, its format is as follows: (Define process name (lambda (parameter ...)))))

We can customize a simple process as follows:

(Define Add5 (Lambda (× 5)))))

This process requires a parameter, which is the value to return this parameter, such as:

(Add5 11) => 16

Below is a simple definition of square process Square:

(Define Square (Lambda (x) (* x))))))

Another way to be the same as lambda

In the Scheme language, you can also use define to define the process without lambda, its format is: (Define) (process content ...))

As follows:

(Define (add6 x) ( x 6)) Add6

# Description Add6 is a process, it has a parameter x

(Add6 23) => 29

Look at the following:

Guile> (Define Fun

(lambda (proc x y)

(Proc x Y)))))))))))

Guile> Fun

#

Guile> (Fun * 5 6)

30

Guile> (FUN / 30 3)

10

More process definitions

The process FUN defined above has three parameters, where the first parameter PROC is also an operation process (because the process is also a data in the Scheme language, the parameters of the process can be used as the parameter of the process, so the above Call the result.

Guile> (Define Add)

(lambda (x y)

( x y)))))))

Guile> Add

#

Guile> (Fun ADD 100 200)

300

Continue to do it, we define a process add, pass the ADD as a parameter to the FUN process, derived (FUN 100 200) the same results.

Guile> ((Lambda (x) ( x)) 5)

10

The above (Lambda (X) ( XX)) is actually a simple process definition, and the operation parameter 5 is directly plus the operation parameter 5, resulting in the result 10, so that the anonymization process, direct process definition to operate the parameters, result Computing result.

Through the above operation, I believe that you have initially learned the usage of the process. Since the process is a data type, it is entirely possible to use the process as a process. The following procedure is the process of determining whether the parameter is a process, gives a parameter, and uses procedure to determine if the parameter is a process, using the IF structure (regarding the IF structure as described below):

Guile> (Define ISP

(lambda (x)

(if (Procedure? x) 'Isaprocedure' NOTAPROCEDURE)))))))))

Guile> ISP

#

Guile> (ISP 0)

NOTAPROCEDURE

Guile> (ISP )

Isaprocedure

The above process reflects the parameters of the Scheme language from the province (identification) capabilities, '0' is a digital type, so returning NOTAPROCEDURE; and ' ' is a most basic operation process, so returns Isaprocedure.

Nested definition of the process

In the Scheme language, the process definition can also nested, in general, the internal process definition of the process is only valid inside the process, and the local variables in the C language.

The final result of the following code is 50:

(Define FIX

(Lambda (x y z)

(Define Add

(Lambda ( A b)))))

(- x (add y z))))))))))

(Display (FIX 100 20 30))

At this time, the process add only uses inside the FIX process, which in fact involves the binding of the process and variables, which can refer to the following information about process bindings (let, let *, and letract).

转载请注明原文地址:https://www.9cbs.com/read-29638.html

New Post(0)