Back Contents Next

5. Standard Entities

This chapter describes Better Scheme's built in entities, that is functions, macro, etc. The initial (or "top level") Better Scheme environment starts out with a number of variables bound to locations containing useful values, most of which are primitive functions or macros that manipulate data. For example, the variable abs is bound to (a location initially containing) a procedure of one argument that computes the absolute value of a number, and the variable + is bound to a procedure that computes sums. Built-in entities that can easily be written in terms of other built-in entities are identified as "library".

A program may use a top-level definition to bind any variable. It may subsequently alter any such binding by an assignment (see section 5.3 Assignment). These operations do not modify the behavior of Scheme's built-in procedures. Altering any top-level binding that has not been introduced by a definition has an unspecified effect on the behavior of the built-in procedures.

5.1 Binding Constructs

5.1.1 Lambda

macro: (lambda (<symbol> ...) <expression1> <expression>...)
macro: (lambda <symbol> <expression1> <expression>...)
macro: (lambda (<symbol_1> ... <symbol_n> . <symbol_n+1>) <expression1> <expression>... )

A lambda instatiation evaluates to a closure. The environment in effect when the lambda was evaluated is remembered as part of the closure. When the function is later called with arguments, the environment in which the lambda expression was evaluated will be extended by binding the variables in the formal argument list to fresh locations, the corresponding actual argument values will be stored in those locations, and the expressions in the body of the lambda expression will be evaluated sequentially in the extended environment. the result(s) of the last expression in the body will be returned as the result(s) of the function call.

(lambda (x) (+ x x)) => a procedure
((lambda (x) (+ x x)) 4) => 8
(define reverse-subtract (lambda (x y) (- y x))) (reverse-subtract 7 10) => 3
(define add4 (let ((x 4)) (lambda (y) (+ x y)))) (add4 6) => 10

should have one of the following forms: * ( ...): The procedure takes a fixed number of arguments; when the procedure is called, the arguments will be stored in the bindings of the corresponding variables. * : The procedure takes any number of arguments; when the procedure is called, the sequence of actual arguments is converted into a newly allocated list, and the list is stored in the binding of the . * ( ... . ): If a space-delimited period precedes the last variable, then the procedure takes n or more arguments, where n is the number of formal arguments before the period (there must be at least one). The value stored in the binding of the last variable will be a newly allocated list of the actual arguments left over after all the other actual arguments have been matched up against the other formal arguments.

It is an error for a <symbol> to appear more than once in the formal parameters.

((lambda x x) 3 4 5 6)		 =>  (3 4 5 6)
((lambda (x y . z) z) 3 4 5 6)		=>  (5 6)

Each procedure created as the result of evaluating a lambda expression is (conceptually) tagged with a storage location, in order to make eqv? and eq? work on procedures (see section see section 6.1 Equivalence predicates).

5.1.2 Mu

5.1.3 Let Constructs

library macro: (let ((<symbol> <init-expression>) ...) <expression1> <expression> ...)
library macro: (let* ((<symbol> <init-expression>) ...) <expression1> <expression> ...)
library macro: (letrec ((<symbol> <init-expression>) ...) <expression1> <expression> ...)

5.1.4 Define

macro: (define <symbol> <expression>)

5.2 Flow Control

library macro: (begin <expression1> <expression> ...)
library macro: (if <test-expression> <consequent-expression> )
library macro: (if <test-expression> <consequent-expression> <alternate-expression> )
library macro: (cond <clause1> <clause> )
where a clause has the form: (<test-expression> <expression> ...)
or alternatively: (<test-expression> => <expression> ...)
the last clause may have the form: (else <expression1> <expression> ...)
library macro: case
library macro: and
library macro: or
library macro:
(do ((<symbol> <init-expression> <step-expression>) ...)
    (
<test> <expression> ...)
    <command-expression> ...)

library macro: (let <function-symbol> ((<symbol> <init-expression>) ...) <expression1> <expression> ...)
library macro: apply
library macro: map
library macro: mapl
library macro: mapr
library macro: for-each
library macro: fold
library macro: foldl
library macro: foldr
library macro: call/cc

5.3 Assignment

macro: (set! <symbol> <expression>)

5.4 Equivalence Predicates

macro: (eq? <expression1> <expression2> ...)
macro: (equal? <expression1> <expression2> ...)

5.5 Numbers

See R5RS section 6.2

5.5.1 Numerical Types

See R5RS section 6.2.1

5.5.2 Exactness

See R5RS section 6.2.2

5.5.3 Implementation Restrictions?

See R5RS section 6.2.3

5.5.4 Numerical Operations

See R5RS section 6.2.5 for defintions of the following functions. See below for a few minor differences from the R5RS standard.

function: (number? entity)
function: (complex? entity)
function: (real? entity)
function: (rational? entity)
function: (integer? entity)

function: (exact? z)
function: (inexact? z)

function: (= z1 z2 z ...)
function: (< x1 x2 x ...)
function: (> x1 x2 x ...)
function: (<= x1 x2 x ...)
function: (>= x1 x2 x ...)

library function: (zero? z)
library function: (positive? x)
library function: (negative? x)
library function: (odd? n)
library function: (even? n)

library function: (max x1 x ...)
library function: (min x1 x ...)

function: (+ z ...)
function: (* z ...)

function: (- z1 z ...)
function: (- z)
function: (/ z1 z ...)
function: (/ z)

Note that the forms of - and / accepting more than two arguments are optional in R5RS but not in Better Scheme.

library function: (abs x)

function: (quotient n1 n2)
function: (remainder n1 n2)
function: (modulo n1 n2)

library function: (gcd n1 ...)
library function: (lcm n1 ...)

function: (numerator q)
function: (denominator q)

function: (floor x)
function: (ceiling x)
function: (truncate x)
function: (round x)

library function: (rationalize x y)

function: (exp z)
function: (log z)
function: (ln z)
function: (sin z)
function: (cos z)
function: (tan z)
function: (asin z)
function: (acos z)
function: (atan z)
function: (atan y x)

Unlike R5RS Scheme the log function computes the log base 10 and the ln function is provided to compute the natural logarithm.

function: (sqrt z)

function: (expt z1 z2)?
function: (pow z1 z2)?

pow is a synonym for expt.

function: (make-rectangular x y)
function: (make-polar x y)
function: (real-part z)
function: (imag-part z)
function: (magnitude z)
function: (angle z)

function: (exact->inexact z)
function: (inexact->exact z)

5.5.5 Numerical Input & Output

See R5RS section 6.2.6 for defintions of the following functions. See below for a few minor differences from the R5RS standard.

function: (number->string z)
function: (number->string z radix)

function: (string->number string)
function: (string->number string radix)

5.6 Other Data Types

This section describes operations on some of Better Scheme's non-numeric data types: booleans, pairs, lists, symbols, characters, strings and vectors.

5.6.1 Booleans

library function: (not obj)
library function: (boolean? obj)

5.6.2 Pairs and Lists

function: (pair? obj)

function: (cons obj1 obj2)

library function: (car pair)
library function: (cdr pair)
library function: (caar pair)
library function: (cadr pair)
library function: ...
library function: (cdddar pair)
library function: (cddddr pair)

library function: (conar obj pair)
library function: (condr obj pair)
library function: (conaar obj pair)
library function: (conadr obj pair)
library function: ...
library function: (condddar obj pair)
library function: (conddddr obj pair)

function: (set-car! pair obj)
function: (set-cdr! pair obj)

library function: (null? obj)
library function: (list? obj)

library function: (list obj ...)

library function: (length list)

library function: (append list ...)
library function: (append! list ...)

library function: (reverse list)
library function: (reverse! list)

library function: (list-tail list k)
library function: (list-ref list k)

library function: (memq obj list)?
library function: (member obj list)?

library function: (assq obj alist)?
library function: (assoc obj alist)?

5.6.3 Symbols

function: (symbol? obj)

function: (symbol->string symbol)
function: (string->symbol string)

5.6.4 Characters

function: (char? obj)

function: (char=? char1 char2)?
function: (char<? char1 char2)?
function: (char>? char1 char2)?
function: (char<=? char1 char2)?
function: (char>=? char1 char2)?

library function: (char-ci=? char1 char2)?
library function: (char-ci<? char1 char2)?
library function: (char-ci>? char1 char2)?
library function: (char-ci<=? char1 char2)?
library function: (char-ci>=? char1 char2)?

library function: (char-alphabetic? char)?
library function: (char-numeric? char)?
library function: (char-whitespace? char)?
library function: (char-upper-case? char)?
library function: (char-lower-case? char)?

library function: (char->integer char)
library function: (integer->char n)

library function: (char-upcase char)
library function: (integer-downcase char)

5.6.5 Strings

function: (string? obj)

function: (make-string k)
function: (make-string k char)

library function: (string char ...)

library function: (string-length string)
library function: (string-ref string k)
library function: (string-set! string k char)

String comparision functions
library function: (substring string start end)

library function: (string-append string start end)

library function: (string->list string)
library function: (list->string list)

library function: (string-copy string)

library function: (string-fill! string char)

5.6.6 Vectors

function: (vector? obj)

function: (make-vector k)
function: (make-vector k fill)

library function: (vector obj ...)

function: (vector-length vector)

function: (vector-ref vector k)
function: (vector-set! vector k obj)

library function: (vector->list vector)
library function: (list->vector list)

library function: (vector-fill! vector fill)

5.6.7 Void

function: void?

5.7 Eval

library macro: eval

5.8 Input and Output

5.? OTHERS

library macro: function?
library macro: delay?
library macro: force
library macro: values
library macro: call-with-values
library macro: dynamic-wind
library macro: quasiquote
library macro: `
macro: case-lambda?
Picks a body based on number of args. macro: (eqv? <expression1> <expression2> ...)?
macro: andmap?
macro: ormap?
bit operations
shift
random
box
unbox
set-box!
box?
name inferencing?
hash-tables
structs
promise?
errors
exceptions
let/cc
regular expressions
threads
thread specfic values
arity (number of args)?
++
--


Back Contents Next

jwalker@cs.oberlin.edu