Scheme language--Brief introduction

Source: Internet
Author: User
Tags define definition

A year ago In fact there is time to read SICP this book, and later for various reasons, has not continued to learn. Since the use of scheme in SICP does not apply much. In the eyes of Java,c++ 's language, scheme is really a very alternative. Now that MIT has given up on using scheme as a SICP language and turned to Python, I am here to learn the book and the language. SICP mainly uses scheme language to solve some problems in computer science. In order to urge themselves, every day can learn. Accumulate a small stream into Jianghai, put their own learning process here, for their own review.

This series contains two parts of scheme language level problems and SICP in the curriculum, two parts complement each other. Start the first section of the scheme language today.

Scheme Brief Introduction

Scheme language is a variant of Lisp language and a kind of table processing language. Its simple syntax and easy learning enable users to focus on solving the problem rather than the language itself.

1.1 Basic types

The basic data types supported by scheme are integers, real numbers, fractions, complex numbers, and the ability to use various operations on these types, such as +-*/%, for these basic types. The string "", marked quote. For quote, (quote exp) denotes exp as a token and is not parsed. Often write ' exp.


List has two types, one ending element is empty table (), (list ' a ' B ' C), equivalent to (cons ' A (cons ' B ())), and a table is an unhealthy table, the footer is the end element, (cons ' A (cons ' B ' c)) such tables The value is (a B.C), which indicates that the subsequent element is the end element of the table.


Operation of the table
Car: Take the first element of a table
CDR: The rest of the table after taking the car, for the normal table, the result is a table, on the abnormal table, the result may be an element.
Cons: Constructs a table, assuming that the second parameter is a table, the first parameter is placed as a total in the second parameter table.
List: Constructs the table, placing each of the parameters as a whole in a single table.
There is also a way to construct a normal table: using quote, i.e. ' as ' (a b c).


1.2 Variable Bindings
(Let (Arg1 val1) (argv2 val2 ...) exp1 exp2 ...)
Variable bindings are only valid in the current let expression, such as (Let ((+ *)) (+ 2 3) =6)
Let operations can be nested, but the bindings are visible only to the let interior, and the outer layer let is not visible.


1.3 Lambda expression
(Lambda (Arg1 ...) exp1 exp2)
From the calculation, (Let (var value) exp1 exp2 ...) = = ((Lambda (var ...) exp1 exp2 ...) value ...)
For the lambda function, the number of references
Assuming that there is only one, there can be a random number of actual arguments, all of which are formatted as a list pass to the function.
Assuming that the form list is an unhealthy list, the corresponding actual participation is mapped by one by one, and then the actual participation is formatted as a list to be passed to. After the formal participation, this also means that the number of actual parameters must be guaranteed the number of parameters. Assuming that the form is a normal list, the actual participation must be mapped by one by one.
So ((lambda (x) x) ' a) = A and ((lambda x x) ' a) = (a)

1.4 Define Definition
Define defines a globally visible, in scheme, the ability to define a process that uses a process that is not defined, which does not cause errors, but assumes that you use it, you will get an error unless you add the referenced procedure.
(Define MyList (lambda x x))


1.5-Bar expression
If expression: (if Cond1 result1 result_other)
Cond expression: (Cond (Test1 exp1) (test2 exp2) ... (Else expn)) can also list all, not else
An OR expression: (or Exp1 exp2 ...)
Not expression: (not exp) makes the #f #t之间转换.


About type inference:
(type?) var) can be derived from the type of Var, the typical type is Null,number,string,list,pair ...
Eqv? Be able to infer whether two values are equal


1.6 Recursive process
A process definition calls itself, the recursive process is generally divided into two parts, the terminating part and the recursive part.


1.7 Map operation
(Map fun argv ...)
The map operation applies the fun to the AGV. and returns a list on each element. Here are two demo examples:
(Map abs ' (1-2 3-4 5-6)) = (1 2 3 4 5 6)

(Define Trans

(Lambda (LST)

(Cons (Map car lst) (Map CDR lst))))

(Trans ' ((A.1) (B.2) (c.3)) = ((a b C) 1 2 3)

1.8 Assignment Operation
The set! is used to set the value of a variable. Variables can be global, and can be local. The variables used by set! must be defined in advance. Can be let can also be define.

Scheme language--Brief introduction

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.