Procedures
A procedure is made by a lambda expression, and a define of the form (define (f . formals) body) is a lambda bound to a name. A procedure is a closure: it keeps the variables of the scopes it was made in, and each call makes new locations for its parameters. Procedures are ordinary values; they can be passed, returned, stored and compared with eqv? (the reports leave unspecified whether two procedures with the same code are eqv?).
Formals
| Formals | Accepts | Example |
|---|---|---|
(a b c) | Exactly three arguments, bound to a, b and c | (lambda (x y) (+ x y)) |
args | Any number, gathered into a list bound to args | (lambda args (length args)) |
(a b . rest) | At least two; the remainder as a list bound to rest | (lambda (f . xs) (map f xs)) |
() | No arguments | (lambda () 1) |
Calling a procedure with the wrong number of arguments is an error. R7RS names no optional or keyword parameters; case-lambda chooses between argument lists, and implementations add their own: #!optional (MIT/GNU, Gambit), #:optional and #:key in lambda* (Guile), #!rest, and SRFI 89 and 177 for keyword arguments.
R7RS says a variable is bound to a fresh location by each call, so that a closure made in a loop sees the values of its own iteration; do and named let bind new variables on every iteration, and a plain set! on a variable the loop shares changes what all the closures see.
The numeric tower
Mathematically numbers form a tower of subtypes, each a subset of the one above: number, complex, real, rational, integer. The predicates number?, complex?, real?, rational? and integer? test them: 3 is an integer and therefore also a rational, a real and a complex number. Scheme numbers that model 3 are the same however they are stored. exact-integer? is true of exact integers only, and exact-rational? exists in some implementations. R6RS adds real-valued?, rational-valued? and integer-valued? for numbers like 3.0+0.0i; in R6RS real?, rational? and integer? are false for such a complex with an inexact zero imaginary part, and R7RS keeps the R5RS meaning.
Exactness
Every number is exact or inexact, a property independent of its type: 3 is exact and 3.0 is inexact. A number is exact if it was written as an exact constant or derived from exact numbers by exact operations; inexactness is contagious. (exact? z) and (inexact? z) test it. (inexact z) and (exact z) convert (R7RS; R5RS called them exact->inexact and inexact->exact, which remain in the R5RS library). Rational operations given exact arguments should give exact results; (/ 3 4) must not be 0, and an implementation without exact rationals may report an implementation restriction. An operation may return an exact result from inexact arguments if it can prove the result is unaffected: (* 0 3.5) may be exact 0. (* 0 +inf.0) may return 0, +nan.0, or fail.
Implementation restrictions
An implementation need not support the whole tower, but must implement a coherent subset consistent with its purposes and the spirit of the language: one in which all numbers are real, or non-real numbers are always inexact, or exact numbers are always integers, is still allowed. It must support exact integers throughout the range of indexes and lengths of lists, vectors, bytevectors and strings, and read an integer constant in that range as exact. R7RS lists the optional features that cond-expand can test: ratios, exact-complex, exact-closed, ieee-float, full-unicode. Fixnums, bignums, ratnums, flonums and compnums are the usual representations, and are not visible in the language, though R6RS defines (rnrs arithmetic fixnums) and (rnrs arithmetic flonums) and SRFI 143 and 144 give R7RS-compatible versions.
Numeric procedures of R7RS-small
| Group | Procedures |
|---|---|
| Type predicates | number? complex? real? rational? integer? exact? inexact? exact-integer?; in the inexact library finite? infinite? nan? |
| Comparison | = < > <= >= with two or more arguments; zero? positive? negative? odd? even?; max min (inexact if any argument is) |
| Arithmetic | + * with any number of arguments; - and / with one argument (negate, reciprocal) or more; abs square |
| Integer division | floor/ floor-quotient floor-remainder truncate/ truncate-quotient truncate-remainder, and the older quotient (= truncate-quotient), remainder (= truncate-remainder) and modulo (= floor-remainder). The divisor must not be zero. The / forms return two values |
| Number theory | gcd lcm (any number of arguments, 0 and 1 results for none); numerator denominator |
| Rounding | floor ceiling truncate round (to even on a tie); rationalize (the simplest rational within a tolerance) |
| Roots and powers | sqrt exact-integer-sqrt (returns the root and the remainder) expt exp log (with an optional base in R7RS) |
| Trigonometry | sin cos tan asin acos atan (one argument, or (atan y x)) |
| Complex numbers (complex library) | make-rectangular make-polar real-part imag-part magnitude angle |
| Conversion | exact inexact; number->string and string->number, both with an optional radix (R7RS allows 2, 8, 10 and 16 only) |
Integer division
The two families differ in how the quotient is rounded: floor rounds toward minus infinity and truncate toward zero. Each satisfies n1 = n2 * nq + nr.
(floor/ 5 2) ; 2 1
(floor/ -5 2) ; -3 1
(floor/ 5 -2) ; -3 -1
(floor/ -5 -2) ; 2 -1
(truncate/ 5 2) ; 2 1
(truncate/ -5 2) ; -2 -1
(truncate/ 5 -2) ; -2 1
(truncate/ -5 -2) ; 2 -1
(truncate/ -5.0 -2) ; 2.0 -1.0
R6RS has a third pair of families, div, mod, div-and-mod and div0, mod0, div0-and-mod0, in which mod is never negative and mod0 is centred on zero; R7RS did not adopt them. SRFI 141 defines a full set of integer division operators.
Special values
R6RS and R7RS add the inexact values +inf.0, -inf.0, +nan.0 (-nan.0 is the same in R7RS) and the negative zero -0.0. Support for them is not required in R7RS. It is an error if any argument of / other than the first is an exact zero; if the first is an exact zero, the result may be an exact zero unless another argument is a NaN. R6RS raises the conditions &no-infinities and &no-nans when a result cannot be represented.
Numeric input and output
(number->string z radix) gives an external representation in that radix (2, 8, 10 or 16; any other is an error) that string->number reads back to an eqv? number, and never includes a radix prefix. For an inexact number in radix 10 it contains a decimal point and uses the minimum number of digits that read back correctly. (string->number string radix) returns a number or #f if the string is not one; in R7RS it may not return #f for a string with an explicit radix prefix, and must agree with read. The syntax is on Lexical structure.
R6RS arithmetic libraries
R6RS puts arithmetic into four libraries beyond (rnrs base):
(rnrs arithmetic fixnums)–fixnum?,fixnum-width,least-fixnum,greatest-fixnum, and the operationsfx=?fx<?fx+fx-fx*fxdivfxmodfxdiv0fxmod0fxandfxiorfxxorfxnotfxiffxarithmetic-shiftfxbit-countfxlengthfxbit-fieldand the rest, which raise&implementation-restrictionwhen the result is not a fixnum.(rnrs arithmetic flonums)–flonum?,real->flonum, andfl=?fl+fl*flabsflfloorflsqrtflexpfllogflsinflatanflexptflnan?flinteger?and the others.(rnrs arithmetic bitwise)–bitwise-andbitwise-iorbitwise-xorbitwise-notbitwise-ifbitwise-bit-countbitwise-lengthbitwise-bit-set?bitwise-copy-bitbitwise-bit-fieldbitwise-arithmetic-shiftand its left and right forms,bitwise-rotate-bit-fieldbitwise-reverse-bit-fieldbitwise-first-bit-setbitwise-copy-bit-field.- The condition types
&no-infinitiesand&no-nanswith their constructors and predicates (make-no-infinities-violation,no-nans-violation?and so on).
R7RS-small has no bitwise operations; SRFI 60 and SRFI 151 supply them, and the viewer colours the R6RS and SRFI 60 names (bitwise-and, arithmetic-shift, bit-count) as functions.
Not described
Known, not yet covered
- Reference: the exact results of each numeric procedure for special arguments (
(expt 0 0),(atan 0 0),(sqrt -4)and the branch cuts of the complex functions) are in the reports and are not described. - Reference: the procedures of the R6RS fixnum, flonum and bitwise libraries are named by group, not each with its arguments. Their names were checked against the index of the R6RS libraries document.
- Reference: SRFI 143, 144, 151 and 160 are not described; the SRFI index was the only source.
See also
- Lexical structure – the number syntax
- Standard libraries –
(scheme inexact),(scheme complex),(rnrs arithmetic ...) - Error handling – the arithmetic conditions