Introduction to Scheme language

Source: Internet
Author: User
Tags define function

Introduction to Scheme language

First heard of Lisp, Stallman's GNU Emacs used Lisp as an embedded language to customize and enhance Emacs. GNU Emacs is a text editor where text is a symbol, and Lisp is invented for symbolic computations, so using Lisp in GNU Emacs is a logical thing to do.

The history of the Lisp language has been long, almost as long as Fortran. In the 1950s, computer scientists first invented the Fortran language for digital computing, and later, for symbolic computing, the Lisp (List Processing) language was developed by John McCarthy of MIT in 1960. The language was originally a programming language designed for table processing and was later widely used to deal with AI problems. The Lisp program is filled with pairs of nested parentheses, which represent recursion. Recursion is one of the basic concepts of mathematics, from the recursive theory, all the functions that can be computed can be classified into various combinations of several basic recursive functions.

In the 1994, many Lisp versions were fairly unified, and the later version was called Common Lisp. Common Lisp contains a very rich library, with only the specification of the language up to thousands of pages, including object-oriented CLOS.

The Scheme language, a modern variant and dialect of Lisp, was born in 1975 and was completed by Gerald J. Sussman and Guy L. Steele of MIT. The scheme language is very short, with a total of only 50 pages, not even the length of the index of the common Lisp specification, but is known as the Queen of the Modern programming language kingdom. It differs from the previous and future versions of LISP implementations, but it is easy to learn and use.

The work that Dsssl needs to do is parse the document, and its design uses the scheme language. This book introduces DocBook monographs, so do not intend to write a scheme Daquan, just want to through the introduction of Dragonfly water to make readers know scheme, can achieve the understanding and simple modification dsssl.

Scheme features

The scheme language has its unique charm and looks at the grammatical features of scheme:

    • Parentheses nested

      The Lisp program is filled with pairs of nested parentheses, which embody the most basic mathematical thought-recursion.

    • Simple syntax

      The scheme language has a short specification, with only 50 pages in total.

    • Functional programming languages

      A function is the so-called first class citizen in this programming language. This means that a function can be passed as easily as an int or float. This is the so-called "functional programming Language", the origin of the word functional.

    • Automatic memory management

      Automatic memory management is not a Java patent.

    • Portability is good

      Scheme Development program is very portable, this is because scheme is an interpretation of the language, on different platforms can have a corresponding interpreter.

    • Suitable for use as a scripting language and embedded language

      The simplicity of the syntax makes the scheme implementation very economical, and a scheme interpreter can be very small. Scheme can be embedded in some tools as a scripting language, such as GNU Emacs.

Other features are that the keyword is not sensitive to capitalization.

Data
    • Digital

      Here are the legal number representations: 47,1/3,2.3,4.3e14,1+3i.

    • Character

      The character prefix needs to be prefixed with #\. The following are legal characters:

      #\a #\a #\b #\b #\space #\newline

    • String

      A string consisting of characters enclosed in double quotation marks. such as: "A little String"

    • Boolean value

      Boolean true and False are represented by #t and #f, respectively.

    • List

      Enclosed in parentheses, you can include any data type called a list. such as: (A little (list of) (lists))

    • Array (vector)

      Use # as a prefix, such as: # (1 2 "string" #\x 5)

    • function (or procedure)

      The function as a data type is the feature of scheme language.

    • Symbol

      A symbol can be a symbol except for any character that cannot begin with a number. such as: Symbols:this-is-a-symbol foo A32 c$23*4&7+3-is-a-symbol-too!

Expressions and FunctionsComments

The semicolon begins a comment. Such as:

(+ 3 1); return 4

Constant-expression
    1. Constant-expression

      A constant expression returns the value itself. Such as:

      3.14; return to 3.14 #t; Returns a Boolean value #t #\c; Returns the character #\c "hi!"; Returns the string "hi!"
    2. References (quotation)

      Syntax: (quote obj) or abbreviated as ' obj '

      (+ 2 3); Returns 5 ' (+ 2 3); Returns the list (+ 2 3) (QUOTE (+ 2 3)); Return list (+ 2 3)
Expression notation

Scheme expressions are written in a special way, and the expressions are enclosed in parentheses. The first line in parentheses is the function name or operator, and the other is the parameter. This type of expression in scheme can be called a front-mounted type. Here are some examples of scheme expressions and their corresponding C-language notation.

  Scheme                                C ------- -----------------------------------------------------------  (+ 2 3 4)                           (2 + 3 + 4)    (< low x high)                     (low <  x)  &&  (X < high)    (+  (* 2 3)                          (* 4 5)   ((2 * 3)  +  (4 * 5))    (f x y)                           f (x, y)    (define  (sq x)   (* x x))      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;SQ (int x)  { return  (x * x)  }

Assignment and Function definitions
  1. Let expressions and Assignments

    Syntax: (Let ((Var val) ...) exp1 exp2 ...)

    Description: The assignment of a let expression is valid only within an expression.

    Example:

    (Let ((x 2) (y 3)) (+ x y))

    ; First assignment: x=2, y=3, and then the value of x+y, the result is 5. Note that (x 2) and (y 3) there is a parenthesis.

    For more examples:

    (Let (f +)) (F 2 3)); Return 5 (Let ((f +) (x 2)) (F x 3)); Return 5 (Let ((f +) (x 2) (y 3)) (f x y)); Return 5
  2. Assigns the

    syntax with define and set!: (define VAR exp)  , (set! var exp)

    Description: The assignment of define and set! expressions is globally valid. Define and set! The difference is that define can both assign and define variables, and set! can only assign values to variables that have already been defined.

    Example:

     (define a 1)    a ; return 1  (set! a  2)  a ; return 2  (let  ((a 3))  a)   ; return 3 a  ; return 2  (let  (a 3)   (set! a 4)  a)  ; return  4 a ; return 2  (let  (a 3)   (define a 5)  a)   ; return 5 a ; return 2  (set! b 1)  ;  error, B has not defined 
  3. lambda Expressions and function definitions

    Syntax: (Lambda (var ...) exp1 exp2 ...)

    Description: A lambda expression is used to define a function. Var... is the parameter, EXP1 exp2 ... is the execution part of the function. It is often necessary to combine a local definition let or a global definition of an expression define, and then make a function call.

    Example:

    (Lambda (x) (+ x x)) (* 3 4)); Return 24

    Description: The function is defined with lambda first, the parameter is x, and the function returns X+X. The statement also completes the function call, where the argument is 12 (equals 3*4), so the return value is 24 (equals 12+12).

  4. Defines a function in a let expression.

    In the scheme language, a function acts as a data type and assigns a lambda expression to the corresponding function through an assignment statement.

    Example:

    (Let (double (lambda (x) (+ x x))) (List (double (* 3 4)) (double (/One)) (Double (-2 7))); Return (24 18-10)

    Description: A let expression assigns a lambda-defined function to a double, and the argument is X, which returns X+X. The double function is then called three times, and the result is returned as a list. The list expression is responsible for generating lists.

  5. The
  6. Defines the function with the Define global definition expression.

    Functions defined with let are valid only in let expressions, and if you want to define function definitions that are valid throughout your program, you need to use the global definition expression--define.

    Example:

     (define double  (lambda  (x)   (+ x x)))   (DOUBLE&NBSP;12)             ; return 24  (double   (* 3 4)        ; return 24 

    Description: The define expression defines a globally valid function double. Two times the return value of the double call is 24.

  7. Defines shorthand for a function

    The syntax of a function defined with define can be simplified, and the lambda will be removed. Upcoming grammar

    (Define VAR0 (lambda (var1 ... varn) E1 E2 ...))

    Abbreviated as:

    (Define (var0 var1 ... varn) E1 E2 ...)

    Example:

    (define (Double x) (+ x x)) (double 12); Return (Double (* 3 4)); Return 24

    Description: This example is a simplified version of the previous example. More brief, clear.

Sequential evaluation Expressions

Syntax: (Begin EXP1 Exp2 ...)

Description: Sequential execution expression exp1, EXP2, ..., returns the result of the last expression

Example:

(Define x 3) (Begin (Set! x (+ x 1)) (+ x x)); Return result 8

Description: Begin expression, first with set! expression for x assignment to 4, in Operation X+x, return result 8.

Conditional expressions
  1. Relational operators

    (<-1 0) #t (>-1 0) #f (eqv? ' A ' a) #t
  2. Logical operations

    (not #t) the #f (not #f) #t (not 1) #f (or) #f (or #f) #f (or #f #f) #t (or #t ' a #f) a (a nd) #t (and #f) #f (and #f #t) #f (and #f ' a #f) #f (and ' a #t ' B) ' B
  3. An If expression

    Syntax: (if test consequent alternative)

    Description: Returns consequent if the test expression is true, otherwise returns alternative.

    Example:

    (Define (ABS N) (if (< n 0) (-0 N) n))

    Description: The function ABS function is to take absolute value.

  4. Cond expression

    Syntax: (COND (Test exp) ... (else exp))

    Description: The Multi-path branch judgment expression, similar to the C language "if ... else if ... else".

    Example:

     (define abs    (lambda  (n)       (cond         ((= n 0)  0)         ((<  n 0)   (- 0 n))         (else n))) 

    Description: The absolute value function ABS is re-implemented with cond expression.

  5. Case expression

    Syntax: (case exp0 clause1 clause2 ...)

    The syntax structure of the clause is: ((Key1 ...) exp1 ...) The structure of the last expression can be: (Else exp1 exp2 ...)

    Description: "Switch ... case ..." similar to the C language Statement.

    Example:

    (Let ((x 4) (y 5)) (case (+ x Y) ((1 3 5 7 9) ' Odd) ((0 2 4 6 8) ' even) (Else ' out-of-range)); Back to Odd

    Description: The case expression first computes the value of X+y to 9, then matches in key and returns the value of the corresponding expression ' odd.

Cycle
  1. Do expression

    Syntax: (Do (var1 val1 update1 ...) (Test res ...) Exp ...)

    Description: A For loop similar to the C language. The VAL1 is assigned to VAR1, ..., then the loop begins, at the beginning of each loop, the expression test is executed first, if the Boolean value is true, then the loop terminates and the result res is returned, if the expression test returns a Boolean value of #f, then the expression exp ... is run, followed by Update1 ... Values for the variable var1 ... Re-assign the value.

    Example 1: Calculate factorial n! = N (n-1)!

    (Define factorial (lambda (n) (Do ((i n (-I 1)) (a 1 (* a i))) ((zero? i))))) (factorial 10); Returns 3628800

    Description: Its corresponding C language implementation is as follows

    long factorial (int n) {int i=n; long a=1; for (i=n;; i--) {if (i = = 0) return A; a *= I;}}

    Example 2: Calculation of the Fibonacci sequence: F (n+1) =f (n) +f (n-1), n>0, F (1) =1, f (0) =0

    (Define Fibonacci (Lambda (n) (if (= n 0) 0 (Do ((i n (-I 1)) (A1 1 (+ A1 A2)) (A2 0 A1))  ((= i 1) a1)))) (Fibonacci 6); Returns 8

    Description: Its corresponding C language implementation is as follows

    Long Fibonacci (int n) {long f=1; int i = n; int a1= 1; int a2= 0; if (n = = 0) return 0; while (1) {if (i = = 1) return A1; i--; A1=A1+A2; A2=A1; } }
  2. Map expression

    Syntax: (Map procedure List1 list2 ...)

    Description: List List1 list2 ... Must have the same length, the procedure procedure the number of arguments that are accepted by the list, and the corresponding variables in each list are executed as parameters of the process procedure, returning each operation result as a list.

    (Map abs ' (1-2 3-4 5-6)); Return (1 2 3 4 5 6), ABS accepts a parameter (map (lambda (x y) (* x y)) ' (1 2 3 4) ' (8 7 6 5)) (8 +), lambda (x y) receives two parameters Number
  3. For-each-expression

    Syntax: (For-each procedure List1 list2 ...)

    Description: The same as the map expression, but does not return a list of results.


Introduction to Scheme language

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.