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.
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).
library macro: (let ((<symbol> <init-expression>) ...) <expression1> <expression> ...)
library macro: (let* ((<symbol> <init-expression>) ...) <expression1> <expression> ...)
library macro: (letrec ((<symbol> <init-expression>) ...) <expression1> <expression> ...)
macro: (define <symbol> <expression>)
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
macro: (set! <symbol> <expression>)
macro: (eq? <expression1> <expression2> ...)
macro: (equal? <expression1> <expression2> ...)
See R5RS section 6.2
See R5RS section 6.2.1
See R5RS section 6.2.2
See R5RS section 6.2.3
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)
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)
This section describes operations on some of Better Scheme's non-numeric data types: booleans, pairs, lists, symbols, characters, strings and vectors.
library function: (not obj)
library function: (boolean? obj)
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)?
function: (symbol? obj)
function: (symbol->string symbol)
function: (string->symbol string)
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)
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)
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)
function: void?
library macro: eval
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)?
++
--
jwalker@cs.oberlin.edu