Scheme Reference

Alexander Schroeder


Table of Contents


Constants

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
binary notation
#o
octal notation
#d
decimal notation (the default)
#x
hexadecimal notation
#e
exact notation
#i
inexact notation (the default for numbers with decimal point, exponent or a `#' in the place of a digit)
5
     => 5
#xff
     => 255

In addition to that, numbers can use the following exponent markers:

s
short precision
f
single precision
d
double precision
l
long precision
s
short precision
e
default precision, usually double
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)

Variables and Procedures

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 ...)
a fixed number of variables
variable
any number of variables, all collected in a list
(variable-1 ... variable-n . variable-n+1)
the dotted list notation specifies a fixed number of variables with all remaining variables collected in a list

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)))))

Assignment and Definition

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.

Boolean

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)
Return #t if obj is either #t or #f.
(not obj)
Return #t if obj is false.

Pairs

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)
Return #t if obj is a pair.
(cons obj-1 obj-2)
Return a newly allocated pair whose car is obj-1 and whose cdr is obj-2.
(car pair)
Return the content of the car field of pair.
(cdr pair)
Return the content of the cdr field of pair.
(set-car! pair obj)
Store ojb in the car field of pair.
(cdr pair obj)
Store ojb in the cdr field of pair.
(caar pair)
(cadr pair)
...
(cdddar pair)
(cddddr pair)
These procedures are combinations of car and cdr. Apply one car for every a and one cdr for every d in the procedure name.

Lists

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)
Return #t if obj is a list.
(null? obj)
Return #t if obj is the empty list '().
(list obj ...)
Return a newly allocated list of its arguments.
(length list)
Return the length of the list. The empty list has length 0.
(append list ...)
Return a list consisting of the elements of all the arguments. The resulting list is always newly allocated, except that it shares structure with the last list argument.
(reverse list)
Return a newly allocated list consisting of the elements of list in reverse order.
(list-tail list k)
Return the sublist of list obtained by omitting the first k elements.
(list-ref list k)
Return the kth element of list.

Lists can be treated like sets.

(memq obj list)
(memv obj list)
(member obj list)
Return the first sublist of list whose car is obj. If obj does not occur in list, then #f is returned. 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)
Return the first pair in alist whose car is obj. If no pair in alist has obj as its car, then #f is returned. 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)
Call procedure proc with the elements of the list (append (list arg-1 ...) args) as the actual arguments.
(map proc list-1 list-2 ...)
Apply procedure 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 ...)
Unlike 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)

Numbers

FIXME

Strings

FIXME

Symbols

FIXME

Vectors

FIXME

Macros

FIXME

Input and Output

FIXME

Tail recursion

FIXME

Missing

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)

Function Index

a

  • append
  • apply
  • assoc
  • assq
  • assv
  • b

  • boolean?
  • c

  • caar
  • cadr
  • car
  • cdddar
  • cddddr
  • cdr
  • cons
  • d

  • define
  • f

  • for-each
  • l

  • lambda
  • length
  • list
  • list-ref
  • list-tail
  • list?
  • m

  • map
  • member
  • memq
  • memv
  • n

  • not
  • null?
  • p

  • pair?
  • q

  • quote
  • r

  • reverse
  • s

  • set!
  • set-car!
  • set-cdr!
  • Concept Index

    a

  • Association lists
  • c

  • Calling procedures
  • Constants
  • Continuations
  • d

  • Defining variables and procedures
  • Dotted list
  • l

  • Local procedures
  • n

  • Numbers, notation
  • p

  • Procedure calls
  • Procedure definition
  • Procedures, anonymous
  • s

  • Sets
  • v

  • Variable definition
  • Variables

  • This document was generated on 17 October 2000 using the texi2html