The most beautiful Programming Language Scheme-more detailed study of the process and higher-order functions

Source: Internet
Author: User

We have mentioned the composite process and explained how to define the composite process in scheme. However, our previous process definition is relatively simple, and we have not even used the so-called local variables in C/C ++. Next we will study the access to global variables and local variables during scheme.

(Note: The process and function concepts are mixed here. We will talk about Lambda later. Actually, lambda is a real function)

 

In scheme, we can access a global variable in a process:

(Define (func X) (+ x g_var) <br/>; Value: func <br/> (func 10) <br/>; unbound variable: g_var <br/>; To continue, call restart with an option number: <br/>; (restart 3) => specify a value to use instead of g_var. <br/>; (restart 2) => define g_var to a given value. <br/>; (restart 1) => return to read-eval-print Level 1. <br/>; Start debugger? (Y or N ): 

We defined a process func above, and then changed the value of the Process Calculation parameter X and the global variable g_var.

When g_var is not defined, the interpreter reports an error, prompting that g_var is not bound.

(Define g_var 100) <br/>; Value: g_var <br/> (func 10) <br/>; Value: 110 

After we define g_var, we call the func process again and get the correct result.

 

In scheme, the characteristics of the local variable scope are the same as those of traditional imperative languages, such as C/C ++, if a local variable with the same name as the global variable is defined in a process, the variable referenced in this process will be a local variable rather than a global variable. For example:

(Define (Proc X) <br/> (define g_var-100) <br/> (+ x g_var) <br/>; value: proc <br/> (Proc 10) <br/>; Value:-90 <br/> (func 10) <br/>; Value: 110 

In the above Code, we define a proc process, which defines a local variable with the same name as the global variable g_var. Finally, we add the parameter X and the local variable g_var to obtain the return value.

From the result, we can see that X is added to the local variable g_var, and thus the result of-90 is obtained.

Let's look at the situation again:

(Define (Proc X) <br/> (define y (/g_var100) <br/> (define g_var (+ Y 1 )) <br/> (+ x y g_var) <br/>; premature reference to reserved name: g_var <br/>; To continue, call restart with an option number: <br/>; (restart 3) => return to read-eval-print Level 3. <br/>; (restart 2) => return to read-eval-print Level 2. <br/>; (restart 1) => return to read-eval-print Level 1. <br/>; Start debugger? (Y or N): n 

We will find this error-"the first use of the reserved name ". What does it mean?

Because we reference the global variable g_var when defining y. When we define the local variable g_var In Proc, We reference the defined y. At this time, the interpreter will use the regular expression to expand the expression of Y for syntax check. It is found that the expression bound to y already uses g_var, but this g_var is already an internal reserved variable name when defining our local variable g_var, rather than the global g_var, so an interpretation error occurs. From this we can see that scheme still expands the variables we defined during the interpretation, checks the syntax of the entire expression, and finally computes the expression.

The following expression is correct:

(Define (Proc X) <br/> (define y (/g_var 100) <br/> (define z (-g_var 1 )) <br/> (+ x y z) <br/>; Value: Proc <br/> (Proc 10) <br/>; Value: 110 

 

As we can see from the above example, the define in scheme is not so much a definition as it is more similar to the macro -- # define in C/C ++. Scheme replaces the variables after define with expressions during syntax analysis, and then performs computation. In this way, scheme has reason to support more powerful functions.

We have learned JavaScript, or C ++ will know the concept of "function object. In JavaScript, a function can return itself. In C, a function pointer variable can be returned. In C ++, a reference to a function can be returned or a function can be encapsulated using a class, the object of this class is called a "function object ".

This will look very natural in scheme:

(Define (high-order-function) <br/> (define (Proc X) (+ x 1) <br/> proc) <br/>; value: high-order-function <br/> (high-order-function) 100) <br/>; Value: 101 

Here, we define a process called high-order-function. In this process, we define an internal process proc, which executes the Add 1 function, and finally returns Proc.

Because when we call the (high-order-function) process, the returned result is proc with a parameter, So we pass the real parameter in and finally get the result. We call this return result a process, or a function whose parameter is a processHigh-order functions(High Order function ).

 

Let's look at a more complex one:

(Define (high-order-function func) <br/> (define (Proc X) (func (-x 1) <br/> proc) <br/>; value: High-Order-function <br/> (define (square X) (* X) <br/>; value: square <br/> (high-order-Function Square) 3) <br/>; Value: 4 

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.