Constants evaluate to themselves. String constants are enclosed within doublequotes ("). Boolean constants are either #t or #f.
"abc"
=> "abc"
145932
=> 145932
#t
=> #t
Number constants may be written using the following prefixes:
#b
#o
#d
#x
#e
#i
5
=> 5
#xff
=> 255
In addition to that, numbers can use the following exponent markers:
s
f
d
l
s
e
3.141592653589793238462643383276e0
=> 3.14159265358979
If you want to include a literal constant in your scheme code that is not a number constant, a string constant, a character constant, or a boolean constant, the expression must be quoted. Quoting is achieved by the following syntax:
(quote datum) 'datum
In either case, the expression evaluates to datum.
(quote (a b c))
=> (a b c)
'(a b c)
=> (a b c)
variable
Variables evaluate to their value. See section Assignment and Definition.
(define x 28)
x
=> 28
Anonymous procedures are created using lambda expressions. See section Assignment and Definition. Procedures are also called combinations.
(lambda formals body)
formals is a list of formal arguments, body should be a sequence of one or more expressions.
Formals should have one of the following forms:
(variable-1 ...)
variable
(variable-1 ... variable-n . variable-n+1)
This is often used for very small procedures. In the following
example, sort requires a procedure as the second argument. The
anonymous procedure is created by the lambda expression.
(define (randomize-list l) (sort l (lambda (a b) (= 1 (random 2)))))
Use set! to change the value of a variable.
(set! variable expression)
The return value is unspecified.
(set! region name)
=> unspecified
Definitions are only valid at the top level of a program and at the beginning of a body.
(define variable expression)
Note that it is an error to try and set! an unbound variable,
but it is not an error to define an unbound variable.
You can use define to define procedures as well as variables.
(define (variable formals) body)
This defines a new procedure. It is equivalent to
(define variable (lambda (formals) body)).
See section Variables and Procedures.
(define (find-in things predicate) (cond ((null? things) #f) ((predicate (car things))) (else (find-in (cdr things) predicate))))
The following also defindes a new procedure.
(define (variable . formal) body)
It is equivalent to
(define (variable) (lambda formal body)).
A procedure call is written by simply enclosing it in parentheses.
(operator operand ...)
The operator and operand expressions are evaluated in an unspecified order before the procedure is called.
The boolean objects are written as #t and #f. Except
for #f, all standard Scheme values, including #t, pairs,
the empty list '(), symbols, numbers, strings, vectors, and
procedures, count as true.
(boolean? obj)
#t if obj is either #t or #f.
(not obj)
#t if obj is false.
A pair (also called a dotted pair) is a record structure with two fields called the car and the cdr fields.
The print representation of a list resembles a procedure call.
(obj-1 . obj-2)
In Scheme code a pair is usually quoted.
'("Alex" . 7)
(pair? obj)
#t if obj is a pair.
(cons obj-1 obj-2)
(car pair)
(cdr pair)
(set-car! pair obj)
(cdr pair obj)
(caar pair)
(cadr pair)
...
(cdddar pair)
(cddddr pair)
car and cdr. Apply
one car for every a and one cdr for every d in the
procedure name.
Lists are made up of pairs. See section Pairs. The car of each pair is an
element in the list, while the cdr of each pair points to the next
pair. The cdr of the last pair points to the empty list '().
The print representation of a list resembles a procedure call.
(obj ...)
In Scheme code a list is usually quoted.
(order "Alex"
'(deal-cancel 57))
(list? obj)
#t if obj is a list.
(null? obj)
#t if obj is the empty list '().
(list obj ...)
(length list)
(append list ...)
(reverse list)
(list-tail list k)
(list-ref list k)
Lists can be treated like sets.
(memq obj list)
(memv obj list)
(member obj list)
Memq uses eq? to compare obj with the elements of
list, while memv uses eqv? and member uses
equal?.
Lists can function as association lists (alist). Each element of an alist is a pair, where the car of the pair is associated with the cdr of the pair.
(assq obj alist)
(assv obj alist)
(assoc obj alist)
Assq uses eq? to compare obj with the car fields
of the pairs in alist, while assv uses eqv? and
assoc uses equal?.
Note that in order to update an entry in an alist, it is generally enough to cons a new pair with the same car and a new cdr to the front of the alist.
Lists can be used for mapping operations.
(apply proc arg-1 ... args)
(append
(list arg-1 ...) args) as the actual arguments.
(map proc list-1 list-2 ...)
proc element-wise to the elements of the
lists and return a list of the results. The procedure
proc must take as many arguments as there are lists and
all the lists must be the same length.
(for-each proc list-1 list-2 ...)
map, for-each is guaranteed to call proc on
the elements of the lists in order from the first element(s) to
the last. The return value is unspecified; for-each is called
for its side effects.
(apply string-append "Kurt"
(map (lambda (s)
(string-append " " s))
'("Alex" "Chris" "Marcel")))
=> "Kurt Alex Chris Marcel"
(for-each (lambda (p)
((p 'register) name)) people)
FIXME
FIXME
FIXME
FIXME
FIXME
FIXME
FIXME
This reference manual does not cover all of the Scheme language specification. Here is a list of what is missing:
(force promise)
(call-with-current-continuation proc)
(values obj ...)
(call-with-values producer consumer)
(dynamic-wind before thunk after)
(eval expression environment-specifier)
(scheme-report-environment version)
(null-environment version)
(interaction-environment)
This document was generated on 17 October 2000 using the texi2html