Scheme (programming language): Difference between revisions

Content deleted Content added
No edit summary
Ap (talk | contribs)
Added short overview of Scheme code with examples
Line 1:
[[ja:Scheme]] [[pl:Scheme]]
 
The '''Scheme programming language''' is a [[functional programming language|functional]] [[programming language]] which is a dialect of [[Lisp]]. It was developed by [[Guy L. Steele]] and [[Gerald Jay Sussman]] in the [[1970s]] and introduced to the academic world via a series of papers now referred to as Sussman and Steele's 'Lambda Papers.'
Line 25:
 
== Standards ==
There are two standards that define the Scheme language: the official IEEE standard, and a de facto standard called the ''Revised<sup>n-th</sup> Report on the Algorithmic Language Scheme'', nearly always abbreviated R<sup>n</sup>RS, where <I>n</I> is the number of the revision. The latest R<sup>n</sup>RS version is R<sup>5</sup>RS, also available online at [http://www.schemers.org/Documents/Standards/R5RS/ online].
 
 
== Language Elements ==
 
=== Variables ===
 
Variables are dynamically typed. Variables are defined by either a define or a let statement. Variables declared outside of any function are in "global" scope.
 
(define var1 value)
 
(let ((var2 value))
...)
 
=== Functions ===
 
Functions are first-class objects in Scheme. They can be assigned to variables. For example a function with two arguments ''arg1'' and ''arg2'' can be defined as
 
(define fun
(lambda (arg1 arg2)
...))
 
which can be abbreviated as follows
 
(define (fun arg1 arg2)
...)
 
Functions can be called with the following syntax. Still using the
 
(fun value1 value2)
 
Note that the function being called is in the first position of the list while the rest of the list contain the arguments. The <code>apply</code> function will take the first argument and ''apply'' the rest of the arguments to the first argument, so the previous function call can also be written as
 
(apply fun (list value1 value2))
 
=== Lists ===
 
Some consider the list Scheme's fundamental data type. A list can be constructed by ''cons''ing elements together. The empty list is denoted as '(). As an example a list containing 1, 2, 3, 4 can be constructed as
 
(cons 1 (cons 2 (cons 3 (cons 4 '()))))
 
which can be abbreviated as
 
(list 1 2 3 4)
 
or as
 
'(1 2 3 4)
 
Note the quote ('), this tells Scheme to ''not'' interpret the following expression.
 
=== Data Types ===
 
Other common data types in Scheme besides functions and lists are: [[integer number|integer]], [[rational number|rational]], [[real number|real]], [[complex number]]s, [[symbol]]s, [[string]]s, [[port]]s. Most Scheme systems also offer [[association list]]s, [[hash table]]s, [[vector]]s, [[array]]s and [[structure]]s.
 
Most Scheme systems offer a full [[numerical tower]] as well as [[exact aritmetic|exact]] and [[inexact aritmetic]].
 
True and false are represented by the symbols #t and #f. Actually only #f is really false when a Boolean type is required, everything else will be interpreted by Scheme as #t including the empty list. This is in contrast with LISP where the empty list is also interpreted as representing false.
 
Symbols can be defined in at least the following ways:
 
'symbol
(string->symbol "symbol")
 
=== Equality ===
 
Scheme has three different types of equality.
 
; eqv?: Return #t if two objects can be considered the same object.
; eq?: Returns #t in mostly the same situations as eqv? but uses some finer distinctions.
; equal?: Compares the content of data structures such as lists, vectors and strings to determine if they are the same.
 
Type dependent equivalence operations also exist in Scheme:
; string=?: To compare two strings
; char=?: To compare characters
; =: To compare numbers
 
=== Control Structures ===
 
==== Conditional evaluation ====
 
(cond (test1 expr1)
(test2 expr2)
...
(else exprn))
 
The first expression for which the test evaluates to #t will be evaluated. If not test results in #t, the else clause is evaluated.
 
A variant of the cond clause is
 
(cond ...
(test => expr)
...)
 
In this case, expr should be a function that takes one argument. If test evaluates to #t, the function is called with the return value of test.
 
Scheme also has
 
(if then-expr else-expr)
 
but is is used much less because <code>cond</code> is both more general and has overall better readability.
 
==== Loops ====
 
Loops in Scheme usually take the form of tail recursion. A classical example is the factorial function:
 
(define (factorial n)
(cond ((= n 0) 1)
(else (* n (factorial (- n 1))))))
 
(factorial 5)
=> 120
 
or a higher order function like ''map'' which applies a function to every element of a list
 
(define (map f lst)
(cond ((null? lst) lst)
(else (cons (f (car lst))
(map f (cdr lst))))))
 
(map (lambda (x) (* x x)) '(1 2 3 4))
=> (1 4 9 16)
 
Another way to define loops is the <code>named let</code> statement and the <code>do</code> statement. To use factorial and map again to illustrate both forms:
 
(define (factorial n)
(let loop ((fact 1)
(n n))
(cond ((= n 0) fact)
(else (loop (* n fact) (- n 1))))))
 
(factorial 5)
=> 120
 
 
(define (map f lst)
(do ((lst lst (cdr lst))
(res '() (cons (f (car lst)) res)))
((null? lst) (reverse res))))
 
(map (lambda (x) (* x x)) '(1 2 3 4))
=> (1 4 9 16)
 
Please note that in both cases, the tail recursive version is preferrable.