Base Library

Library (rnrs base (6))

[R6RS] This library exports many of the procedure and syntax bindings that are traditionally associated with Scheme.

Variable definitions

[R6RS] The define form is a definition used to create variable bindings and may appear anywhere other definitions may appear.

The first from of define binds variable to a new location before assigning the value of expression to it.

(define add3 (lambda (x) (+ x 3)))

(add3 3)
6

(define first car)

(first '(1 2))
1

The second form of define is equivalent to

(define _variable_ _unspecified_)

where unspecified is a side-effect-free expression returning an unspecified value.

In the third form of define, formals must be either a sequence of zero or more variables, or a sequence of one or more variables followed by a dot . and another variable. This form is equivalent to

(define _variable_ (lambda (_formals_) _body ..._))

In the fourth form of define, formal must be a single variable. This form is equivalent to

(define _variable_ (lambda _formal_ _body ..._))

Syntax definitions

[R6RS] The define-syntax form is a definition used to create keyword bindings and may appear anywhere other definitions may appear.

Binds keyword to the value of expression, which must evaluate, at macro-expansion time, to a transformer.

Quotation

[R6RS] quote evaluates to the datum value represented by datum.

(quote a)
a

(quote #(a b c))
#(a b c)

(quote (+ 1 2))
(+ 1 2)

Procedures

[R6RS+]A lambda expression evaluates to a procedure. The environment in effect when the lambda expression is evaluated is remembered as part of the procedure. When the procedure is later called with some arguments, the environment in which the lambda expression was evaluated is extended by binding the variables in the parameter list to fresh locations, and the resulting argument values are stored in those locations. Then, the expressions in the body of the lambda expression are evaluated sequentially in the extended environment. The results of the last expression in the body are returned as the results of the procedure call.

(lambda (x) (+ x x))
a procedure

((lambda (x) (+ x x)) 4)
8

((lambda (x)
   (define (p y) (+ y 1))
   (+ (p x) x)) 5)
11

(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

Formals must have one of the following forms:

Conditionals

[R6RS]An if expression is evaluated as follows: first, _test_is evaluated. If it yields a true value, then consequent is evaluated and its values are returned. Otherwise alternate is evaluated and its values are returned. If test yields #f and no alternate is specified, then the result of the expression is unspecified.

Assignment

[R6RS] Expression is evaluated, and the resulting value is stored in the location to which variable is bound. Variable must be bound either in some region enclosing the set! expression or at the top level. The result of the set! expression is unspecified.

Note: R6RS requires to throw syntax violation if variable refers immutable binding. In Sagittarius, however, it won't throw any error.

Derived conditionals

[R6RS] Each clause must be the form

(_test_ _expression_ ...)

(_test_ => _expression_)

(else _expression_ ...)

The last form can appear only in the last clause.

A cond expression is evaluated by evaluating the test expressions of successive clauses in order until one of them evaluates to a true value. When a test evaluates to a true value, then the remaining expressions in its clause are evaluated in order, and the results of the last expression in the clause are returned as the results of the entire cond expression. If the selected clause contains only the test and no expressions, then the value of the test is returned as the result. If the selected clause uses the => alternate form, then the expression is evaluated. Its value must be a procedure. This procedure should accept one argument; it is called on the value of the test and the values returned by this procedure are returned by the cond expression. If all tests evaluate to #f, and there is no else clause, then the conditional expression returns unspecified values; if there is an else clause, then its expressions are evaluated, and the values of the last one are returned.

[R6RS] Key must be an expression. Each clause must have one of the following forms:

((_datum_ ...) _expression_ ...)

(else _expression_ ...)

The last form can appear only in the last clause.

A case expression is evaluated as follows. Key is evaluated and its result is compared using eqv? against the data represented by the _datums_of each clause in turn, proceeding in order from left to right through the set of clauses. If the result of evaluating key is equivalent to a datum of a clause, the corresponding expressions are evaluated from left to right and the results of the last expression in the clause are returned as the results of the case expression. Otherwise, the comparison process continues. If the result of evaluating key is different from every datum in each set, then if there is an else clause its expressions are evaluated and the results of the last are the results of the case expression; otherwise the case expression returns unspecified values.

[R6RS] If there are no tests, #t is returned. Otherwise, the test expressions are evaluated from left to right until a _test_returns #f or the last test is reached. In the former case, the and expression returns #f without evaluating the remaining expressions. In the latter case, the last expression is evaluated and its values are returned.

(and (= 2 2) (> 2 1))
#t

(and (= 2 2) (< 2 1))

(and 1 2 'c '(f g))
(f g)

(and)
#t

[R6RS] If there are no tests, #f is returned. Otherwise, the _test_expressions are evaluated from left to right until a test returns a true value or the last test is reached. In the former case, the or expression returns val without evaluating the remaining expressions. In the latter case, the last expression is evaluated and its values are returned.

(or (= 2 2) (> 2 1))
#t

(or (= 2 2) (< 2 1))
#t

(or #f #f #f)

(or '(b c) (/ 3 0))
(b c)

Binding constructs

[R6RS] Bindings must have the form

((_variable1_ _init1_) ...)

where each init is an expression. Any variable must not appear more than once in the variables.

The inits are evaluated in the current environment, the variables are bound to fresh locations holding the results, the body is evaluated in the extended environment, and the values of the last expression of body are returned. Each binding of a variable has body as its region.

[R6RS] Bindings must have the form

((_variable1_ _init1_) ...)

The let* form is similar to let, but the inits are evaluated and bindings created sequentially from left to right, with the region of each binding including the bindings to its right as well as body. Thus the second init is evaluated in an environment in which the first binding is visible and initialized, and so on.

[R6RS] Bindings must have the form

((_variable1_ _init1_) ...)

where each init is an expression. Any variable must not appear more than once in the variables.

The variables are bound to fresh locations, the inits are evaluated in the resulting environment, each variable is assigned to the result of the corresponding init, the body is evaluated in the resulting environment, and the values of the last expression in body are returned. Each binding of a variable has the entire letrec expression as its region, making it possible to define mutually recursive procedures.

In the most common uses of letrec, all the inits are lambdaexpressions and the restriction is satisfied automatically.

[R6RS] Bindings must have the form

((_variable1_ _init1_) ...)

where each init is an expression. Any variable must not appear more than once in the variables.

The variables are bound to fresh locations, each variable is assigned in left-to-right order to the result of evaluating the corresponding init, the body is evaluated in the resulting environment, and the values of the last expression in body are returned. Despite the left-to-right evaluation and assignment order, each binding of a variable has the entire letrec*expression as its region, making it possible to define mutually recursive procedures.

[R6RS] Mv-bindings must have the form

((_formals_ _init1_) ...)

where each init is an expression. Any variable must not appear more than once in the set of formals.

The inits are evaluated in the current environment, and the variables occurring in the formals are bound to fresh locations containing the values returned by the inits, where the formals are matched to the return values in the same way that the formals in a lambda expression are matched to the arguments in a procedure call. Then, the body is evaluated in the extended environment, and the values of the last expression of _body_are returned. Each binding of a variable has body as its region. If the formals do not match, an exception with condition type &assertionis raised.

[R6RS] Mv-bindings must have the form

((_formals_ _init1_) ...)

where each init is an expression. In each formals, any variable must not appear more than once.

The let*-values form is similar to let-values, but the _inits_are evaluated and bindings created sequentially from left to right, with the region of the bindings of each formals including the bindings to its right as well as body. Thus the second init is evaluated in an environment in which the bindings of the first formals is visible and initialized, and so on.

Sequencing

[R6RS]The begin keyword has two different roles, depending on its context:

Equivalence predicates

A predicate is a procedure that always returns a boolean value (#t or #f). An equivalence predicate is the computational analogue of a mathematical equivalence relation (it is symmetric, reflexive, and transitive). Of the equivalence predicates described in this section, eq? is the finest or most discriminating, and equal? is the coarsest. The eqv? predicate is slightly less discriminating than eq?.

[R6RS] eq? only sees if the given two objects are the same object or not, eqv? compares numbers. equal? compares the values equivalence.

On Sagittarius Scheme interned symbol, keyword(only compatible mode), character, literal string, boolean, fixnum, and '() are used as the same objects. If these objects indicates the same value then eq? returns #t.

The following examples are not specified R6RS. But it is always good to know how it works.

(let ((p (lambda (x) x))) (eqv? p p))
#t

(eqv? "" "")
#t

(eqv? "abc" "abc") ;; literal string are the same object
#t

(eqv? "abc" (list->string '(#\a #\b #\c)))

(eqv? '#() '#())

(eqv? (lambda (x) x) (lambda (x) x))

(eqv? (lambda (x) x) (lambda (y) y))

(eqv? +nan.0 +nan.0)

Procedure predicate

[R6RS] Returns #t if obj is a procedure, otherwise returns #f.

Numerical type predicates

Function number? obj
Function real? obj

[R6RS] These numerical type predicates can be applied to any kind of argument. They return #t if the object is a number object of the named type, and #f otherwise. In general, if a type predicate is true of a number object then all higher type predicates are also true of that number object. Consequently, if a type predicate is false of a number object, then all lower type predicates are also false of that number object.

If z is a complex number object, then (real? _z_) is true if and only if (zero? (imag-part _z_)) and (exact? (imag-part _z_))are both true.

If x is a real number object, then (rational? _x_) is true if and only if there exist exact integer objects k1 and k2 such that (= _x_ (/ _k1_ _k2_)) and (= (numerator _x_) _k1_)and (= (denominator _x_) _k2_) are all true. Thus infinities and NaNs are not rational number objects.

If q is a rational number objects, then (integer? _q_) is true if and only if (= (denominator _q_) 1) is true. If q is not a rational number object, then (integer? _q_) is #f.

[R6RS] These numerical type predicates can be applied to any kind of argument. The real-valued? procedure returns #t if the object is a number object and is equal in the sense of = to some real number object, or if the object is a NaN, or a complex number object whose real part is a NaN and whose imaginary part is zero in the sense of zero?. The rational-valued? and integer-valued?procedures return #t if the object is a number object and is equal in the sense of = to some object of the named type, and otherwise they return #f.

Function exact? obj

[R6RS] These numerical predicates provide tests for the exactness of a quantity. For any number object, precisely one of these predicates is true.

Generic conversion

Function exact z
Function inexact z

[R6RS] The inexact procedure returns an inexact representation of z. If inexact number objects of the appropriate type have bounded precision, then the value returned is an inexact number object that is nearest to the argument.

The exact procedure returns an exact representation of z. The value returned is the exact number object that is numerically closest to the argument; in most cases, the result of this procedure should be numerically equal to its argument.

Arithmetic operations

[R6RS] These procedures return #t if their arguments are (respectively): equal, monotonically increasing, monotonically decreasing, monotonically nondecreasing, or monotonically nonincreasing, and #f otherwise.

Function zero? z
Function positive? z
Function negative? z
Function odd? z
Function even? z
Function finite? z
Function infinite? z
Function nan? z

[R6RS] These numerical predicates test a number object for a particular property, returning #t or #f.

The zero? procedure tests if the number object is = to zero.

The positive? tests whether it is greater than zero.

The negative? tests whether it is less than zero.

The odd? tests whether it is odd.

The even? tests whether it is even

The finite? tests whether it is not an infinity and not a NaN.

The infinite? tests whether it is an infinity.

The nan? tests whether it is a NaN.

[R6RS] These procedures return the maximum or minimum of their arguments.

Function + z ...
Function * z ...

[R6RS] These procedures return the sum or product of their arguments.

Function - z ...
Function - z1 z2 ...

[R6RS] With two or more arguments, this procedures returns the difference of its arguments, associating to the left. With one argument, however, it returns the additive inverse of its argument.

If this procedure is applied to mixed non-rational real and non-real complex arguments, it returns an unspecified number object.

Function / z ...
Function / z1 z2 ...

[R6RS+] If all of the arguments are exact, then the divisors must all be nonzero. With two or more arguments, this procedure returns the quotient of its arguments, associating to the left. With one argument, however, it returns the multiplicative inverse of its argument.

If this procedure is applied to mixed non-rational real and non-real complex arguments, it returns an unspecified number object.

In R6RS, it requires to raise &assertion when the divisor was 0, on Sagittarius, however, it returns NaN or infinite number when it is running with compatible mode. In R6RS mode it raises &assertion.

Function abs x

[R6RS] Returns the absolute value of its argument.

Function div x1 x2
Function mod x1 x2
Function div0 x1 x2
Function mod0 x1 x2

[R6RS] These procedures implement number-theoretic integer division and return the results of the corresponding mathematical operations. In each case, x1 must be neither infinite nor a NaN, and x2 must be nonzero; otherwise, an exception with condition type &assertion is raised.

Function gcd n1 ...
Function lcm n1 ...

[R6RS] These procedures return the greatest common divisor or least common multiple of their arguments. The result is always non-negative.

Function numerator q

[R6RS] These procedures return the numerator or denominator of their argument; the result is computed as if the argument was represented as a fraction in lowest terms. The denominator is always positive. The denominator of 0 is defined to be 1.

Function floor x
Function ceiling x
Function truncate x
Function round x

[R6RS] These procedures return inexact integer objects for inexact arguments that are not infinities or NaNs, and exact integer objects for exact rational arguments. For such arguments, floor returns the largest integer object not larger than x. The ceiling procedure returns the smallest integer object not smaller than x. The truncate procedure returns the integer object closest to x whose absolute value is not larger than the absolute value of x. The round procedure returns the closest integer object to x, rounding to even when x represents a number halfway between two integers.

Although infinities and NaNs are not integer objects, these procedures return an infinity when given an infinity as an argument, and a NaN when given a NaN.

[R6RS] The rationalize procedure returns the a number object representing the simplest rational number differing from x1 by no more than x2. A rational number r1 is simpler than another rational number r2 if r1 = p1/q1 and r2 = p2/q2 (in lowest terms) and |p1| ≤ |p2| and |q1| ≤ |q2|. Thus 3/5 is simpler than 4/7. Although not all rationals are comparable in this ordering (consider 2/7 and 3/5) any interval contains a rational number that is simpler than every other rational number in that interval (the simpler 2/5 lies between 2/7 and 3/5). Note that 0 = 0/1 is the simplest rational of all.

Function exp z
Function log z
Function log z1 z2
Function sin z
Function cos z
Function tan z
Function asin z
Function acos z
Function atan z
Function atan z1 z2

[R6RS] These procedures compute the usual transcendental functions. The exp procedure computes the base-e exponential of z.

The log procedure with a single argument computes the natural logarithm of z (not the base-ten logarithm); (log _z1_ _z2_) computes the base-z2 logarithm of z1.

The asin, acos, and atan procedures compute arcsine, arccosine, and arctangent, respectively.

The two-argument variant of atan computes (angle (make-rectangular _x2_ _x1_)).

Function sqrt z

[R6RS] Returns the principal square root of z. For rational z, the result has either positive real part, or zero real part and non-negative imaginary part.

The sqrt procedure may return an inexact result even when given an exact argument.

Function expt z1 z2

[R6RS] Returns z1 raised to the power z2. For nonzero z1, this is e^{z2 log z1}. 0.0z is 1.0 if z = 0.0, and 0.0 if (real-part _z_) is positive. For other cases in which the first argument is zero, an unspecified number object(+nan.0+nan.0i) is returned.

For an exact real number object z1 and an exact integer object z2, (expt _z1_ _z2_) must return an exact result. For all other values of z1 and z2, (expt _z1_ _z2_) may return an inexact result, even when both z1 and z2 are exact.

Function real-part z
Function imag-part z
Function magnitude z
Function angle z

[R6RS] Suppose a1, a2, a3, and a4 are real numbers, and c is a complex number such that the following holds:

_c = a1 + a2 ^ i = a3 e ^ ia4_Then, if x1, x2, x3, and x4 are number objects representing a1, a2, a3, and a4, respectively, (make-rectangular _x1_ _x2_) returns c, and (make-polar _x3_ _x4_) returns c.

Numerical input and output

[R6RS+] Radix must be an exact integer object, between 2, and 32. If omitted, radix defaults to 10. In Sagittarius precision will be ignored. The number->string procedure takes a number object and a radix and returns as a string an external representation of the given number object in the given radix such that

(let ((number _z_) (radix _radix_))
  (eqv? (string->number
          (number->string number radix)
          radix)
        number))

is true.

[R6RS+] Returns a number object with maximally precise representation expressed by the given string. Radix must be an exact integer object, between 2, and 32. If supplied, radix is a default radix that may be overridden by an explicit radix prefix in string (e.g., "#o177"). If radix is not supplied, then the default radix is 10. If string is not a syntactically valid notation for a number object or a notation for a rational number object with a zero denominator, then string->numberreturns #f.

These number->string and string->number's resolutions of radix are taken from Gauche.

Booleans

Function not obj

[R6RS] Returns #t if obj is #f, and returns #f otherwise.

[R6RS] Returns #t if obj is either #t or #f and returns #f otherwise.

[R6RS] Returns #t if the booleans are the same.

Pairs and lists

A pair is a compound structure with two fields called the car and cdr fields (for historical reasons). Pairs are created by the procedure cons. The car and cdr fields are accessed by the procedures car and cdr.

Pairs are used primarily to represent lists. A list can be defined recursively as either the empty list or a pair whose cdr is a list. More precisely, the set of lists is defined as the smallest set X such that

The objects in the car fields of successive pairs of a list are the elements of the list. For example, a two-element list is a pair whose car is the first element and whose cdr is a pair whose car is the second element and whose cdr is the empty list. The length of a list is the number of elements, which is the same as the number of pairs.

The empty listis a special object of its own type. It is not a pair. It has no elements and its length is zero.

A chain of pairs not ending in the empty list is called an improper list. Note that an improper list is not a list. The list and dotted notations can be combined to represent improper lists:

(a b c . d)

is equivalent to

(a . (b . (c . d)))

Whether a given pair is a list depends upon what is stored in the cdr field.

Function pair? obj

[R6RS] Returns #t if obj is a pair, and otherwise returns #f.

[R6RS] Returns a newly allocated pair whose car is obj1 and whose cdr is obj2. The pair is guaranteed to be different (in the sense of eqv?) from every existing object.

Function car pair
Function cdr pair

[R6RS] Returns the contents of the car/cdr field of pair.

Function caar pair
Function cadr pair

:

Function cadddr pair
Function cddddr pair

[R6RS] These procedures are compositions of car and cdr, where for example caddr could be defined by

(define caddr (lambda (x) (car (cdr (cdr x)))))

.

Arbitrary compositions, up to four deep, are provided. There are twenty-eight of these procedures in all.

Function null? obj

[R6RS] Returns #t if obj is the empty list, #f otherwise.

Function list? obj

[R6RS] Returns #t if obj is a list, #f otherwise. By definition, all lists are chains of pairs that have finite length and are terminated by the empty list.

[R6RS] Returns a newly allocated list of its arguments.

Function length list

[R6RS+] Returns the length of list. If the list is improper list, it returns -1 If the list is infinite list , it returns -2.

[R6RS] Returns a possibly improper list consisting of the elements of the first list followed by the elements of the other lists, with _obj_as the cdr of the final pair. An improper list results if obj is not a list.

[R6RS] Returns a newly allocated list consisting of the elements of list in reverse order.

[R6RS+] The list-tail procedure returns the subchain of pairs of list obtained by omitting the first k elements. If _fallback_is given and k is out of range, it returns fallback otherwise &assertion is raised.

[R6RS+] The list-ref procedure returns the _k_th element of list. If fallback is given and k is out of range, it returns fallback otherwise &assertion is raised.

[R6RS+ SRFI-1] The map procedure applies proc element-wise to the elements of the lists and returns a list of the results, in order. The order in which proc is applied to the elements of the lists is unspecified. If multiple returns occur from map, the values returned by earlier returns are not mutated. If the given lists are not the same length, when the shortest list is processed the map will stop.

[R6RS+ SRFI-1] The for-each procedure applies proc element-wise to the elements of the lists for its side effects, in order from the first elements to the last. The return values of for-each are unspecified. If the given lists are not the same length, when the shortest list is processed the for-each will stop.

Symbols

Function symbol? obj

[R6RS] Returns #t if obj is a symbol, otherwise returns #f.

[R6RS] Returns the name of symbol as an immutable string.

[R6RS] Returns #t if the symbols are the same, i.e., if their names are spelled the same.

[R6RS] Returns the symbol whose name is string.

Characters

Characters are objects that represent Unicode scalar values.

Function char? obj

[R6RS] Returns #t if obj is a character, otherwise returns #f.

[R6RS] Sv must be a Unicode scalar value, i.e., a non-negative exact integer object in [0, #xD7FF] ∪ [#xE000, #x10FFFF].

Given a character, char->integer returns its Unicode scalar value as an exact integer object. For a Unicode scalar value sv, integer->charreturns its associated character.

[R6RS] These procedures impose a total ordering on the set of characters according to their Unicode scalar values.

Strings

Strings are sequences of characters. The length of a string is the number of characters that it contains. This number is fixed when the string is created. The valid indices of a string are the integers less than the length of the string. The first character of a string has index 0, the second has index 1, and so on.

Function string? obj

[R6RS] Returns #t if obj is a string, otherwise returns #f.

[R6RS] Returns a newly allocated string of length k. If _char_is given, then all elements of the string are initialized to char, otherwise the contents of the string are #\space.

These are equivalence:

(make-string 10)
(code (make-string 10 #\\space))

[R6RS] Returns a newly allocated string composed of the arguments.

[R6RS] Returns the number of characters in the given string as an exact integer object.

[R6RS+] The string-ref procedure returns character. If a third argument is given ant k is not a valid index, it returns fallback, otherwise raises &assertion.

k of string using zero-origin indexing.

[R6RS] Returns #t if the strings are the same length and contain the same characters in the same positions. Otherwise, the string=? procedure returns #f.

[R6RS] These procedures are the lexicographic extensions to strings of the corresponding orderings on characters. For example, string<? is the lexicographic ordering on strings induced by the ordering char<? on characters. If two strings differ in length but are the same up to the length of the shorter string, the shorter string is considered to be lexicographically less than the longer string.

[R6RS] String must be a string, and start and end must be exact integer objects satisfying

0 ≤ startend(string-length _string_).

The substring procedure returns a newly allocated string formed from the characters of string beginning with index start (inclusive) and ending with index end (exclusive).

[R6RS] Returns a newly allocated string whose characters form the concatenation of the given strings.

[R6RS+] List must be a list of characters.

The string->list procedure returns a newly allocated list of the characters that make up the given string.

The list->string procedure returns a newly allocated string formed from the characters in list.

The string->list and list->string procedures are inverses so far as equal? is concerned.

If optional argument start and end are given, it restrict the conversion range. It convert from start (inclusive) to end(exclusive).

If only start is given, then the end is the length of given string.

[R6RS+] Proc should accept as many arguments as there are strings. The string-for-each procedure applies proc element-wise to the characters of the strings for its side effects, in order from the first characters to the last. The return values of string-for-each are unspecified.

Analogous to for-each.

[R6RS+] Returns a newly allocated copy of the given string.

If optional argument start was given, the procedure copies from the given start index.

If optional argument end was given, the procedure copies to the given end index (exclusive).

Vectors

Vectors are heterogeneous structures whose elements are indexed by integers. A vector typically occupies less space than a list of the same length, and the average time needed to access a randomly chosen element is typically less for the vector than for the list.

The length of a vector is the number of elements that it contains. This number is a non-negative integer that is fixed when the vector is created. The valid indices of a vector are the exact non-negative integer objects less than the length of the vector. The first element in a vector is indexed by zero, and the last element is indexed by one less than the length of the vector.

Function vector? obj

[R6RS] Returns #t if obj is a vector. Otherwise returns #f.

[R6RS] Returns a newly allocated vector of k elements. If a second argument is given, then each element is initialized to fill. Otherwise the initial contents of each element is unspecified.

[R6RS] Returns a newly allocated vector whose elements contain the given arguments. Analogous to list.

[R6RS] Returns the number of elements in vector as an exact integer object.

[R6RS+] The vector-ref procedure returns the contents of element k of vector. If a third argument is given and k is not a valid index, it returns fallback, otherwise raises &assertion.

[R6RS] K must be a valid index of vector. The vector-set!procedure stores obj in element k of vector, and returns unspecified values.

It raises &assertion when it attempt to modify immutable vector on R6RS mode.

[R6RS+] The vector->list procedure returns a newly allocated list of the objects contained in the elements of vector. The list->vectorprocedure returns a newly created vector initialized to the elements of the list list. The optional start and end arguments limit the range of the source.

(vector->list '#(1 2 3 4 5))
(1 2 3 4 5)

(list->vector '(1 2 3 4 5))
#(1 2 3 4 5)

(vector->list '#(1 2 3 4 5) 2 4)
(3 4)

(list->vector (circular-list 'a 'b 'c) 1 6)
#(b c a b c)

[R6RS+] Stores fill in every element of vector and returns unspecified values. Optional start and end limits the range of effect between start-th index (inclusive) to end-th index (exclusive). Start defaults to zero, and end defaults to the length of vector.

[R6RS+] Proc should accept as many arguments as there are vectors. The vector-map procedure applies proc element-wise to the elements of the vectors and returns a vector of the results, in order. If multiple returns occur from vector-map, the return values returned by earlier returns are not mutated.

Analogous to map.

[R6RS+] Proc should accept as many arguments as there are vectors. The vector-for-each procedure applies proc element-wise to the elements of the vectors for its side effects, in order from the first elements to the last. The return values of vector-for-each are unspecified.

Analogous to for-each.

Errors and violations

[R6RS] Who must be a string or a symbol or #f. Message must be a string. The irritants are arbitrary objects.

These procedures raise an exception. The error procedure should be called when an error has occurred, typically caused by something that has gone wrong in the interaction of the program with the external world or the user. The assertion-violation procedure should be called when an invalid call to a procedure was made, either passing an invalid number of arguments, or passing an argument that it is not specified to handle.

The who argument should describe the procedure or operation that detected the exception. The message argument should describe the exceptional situation. The irritants should be the arguments to the operation that detected the operation.

Control features

[R6RS] Rest-args must be a list. Proc should accept n arguments, where n is number of args plus the length of rest-args. The applyprocedure calls proc with the elements of the list (append (list _arg1_ ...) _rest-args_) as the actual arguments.

If a call to apply occurs in a tail context, the call to proc is also in a tail context.

[R6RS] Proc should accept one argument. The procedure call-with-current-continuation (which is the same as the procedure call/cc) packages the current continuation as an "escape procedure" and passes it as an argument to proc. The escape procedure is a Scheme procedure that, if it is later called, will abandon whatever continuation is in effect a that later time and will instead reinstate the continuation that was in effect when the escape procedure was created. Calling the escape procedure may cause the invocation of before and after procedures installed using dynamic-wind.

The escape procedure accepts the same number of arguments as the continuation of the original call to call-with-current-continuation.

[R6RS] Returns objs as multiple values.

[R6RS] Producer must be a procedure and should accept zero arguments. Consumer must be a procedure and should accept as many values as producer returns. The call-with-values procedure calls _producer_with no arguments and a continuation that, when passed some values, calls the consumer procedure with those values as arguments. The continuation for the call to consumer is the continuation of the call to call-with-values.

If a call to call-with-values occurs in a tail context, the call to consumer is also in a tail context.

[R6RS] Before, thunk, and after must be procedures, and each should accept zero arguments. These procedures may return any number of values. The dynamic-wind procedure calls thunk without arguments, returning the results of this call. Moreover, dynamic-wind calls _before_without arguments whenever the dynamic extent of the call to thunk is entered, and after without arguments whenever the dynamic extent of the call to thunk is exited. Thus, in the absence of calls to escape procedures created by call-with-current-continuation, dynamic-wind calls before, thunk, and after, in that order.

Iteration

[R6RS] “Named let” is a variant on the syntax of let that provides a general looping construct and may also be used to express recursion. It has the same syntax and semantics as ordinary let except that _variable_is bound within body to a procedure whose parameters are the bound variables and whose body is body. Thus the execution of body may be repeated by invoking the procedure named by variable.

Quasiquote

Auxiliary Syntax unquote qq-template

[R6RS] "Quasiquote" expressions is useful for constructing a list or vector structure when some but not all of the desired structure is known in advance.

Qq-template should be as specified by the grammar at the end of this entry.

If no unquote or unquote-splicing forms appear within the qq-template, the result of evaluating (quasiquote _qq-template_)is equivalent to the result of evaluating (quote _qq-template_).

If an (unquote _expression_ ...) form appears inside a qq-template, however, the expressions are evaluated ("unquoted") and their results are inserted into the structure instead of the unquote form.

If an (unquote-splicing _expression_ ...) form appears inside a qq-template, then the expressions must evaluate to lists; the opening and closing parentheses of the lists are then "stripped away" and the elements of the lists are inserted in place of the unquote-splicing form.

Any unquote-splicing or multi-operand unquote form must appear only within a list or vector qq-template.

Note: even though unquote and unquote-splicing are bounded, however it does not work with import prefix nor renamed import. This may be fixed in future.

Binding constructs for syntactic keywords

The let-syntax and letrec-syntax forms bind keywords. On R6RS mode it works like a begin form, a let-syntax or letrec-syntaxform may appear in a definition context, in which case it is treated as a definition, and the forms in the body must also be definitions. A let-syntaxor letrec-syntax form may also appear in an expression context, in which case the forms within their bodies must be expressions.

[R6RS] Bindings must have the form

((_keyword_ _expression_) ...)

Each keyword is an identifier, and each expression is an expression that evaluates, at macro-expansion time, to a transformer. Transformers may be created by syntax-rules or identifier-syntax or by one of the other mechanisms described in library chapter on "syntax-case". It is a syntax violation for keyword to appear more than once in the list of keywords being bound.

The forms are expanded in the syntactic environment obtained by extending the syntactic environment of the let-syntax form with macros whose keywords are the keywords, bound to the specified transformers. Each binding of a keyword has the forms as its region.

[R6RS] Bindings must have the form

((_keyword_ _expression_) ...)

Each keyword is an identifier, and each expression is an expression that evaluates, at macro-expansion time, to a transformer. Transformers may be created by syntax-rules or identifier-syntax or by one of the other mechanisms described in library chapter on "syntax-case". It is a syntax violation for keyword to appear more than once in the list of keywords being bound.

The forms are expanded in the syntactic environment obtained by extending the syntactic environment of the letrec-syntax form with macros whose keywords are the keywords, bound to the specified transformers. Each binding of a keyword has the bindings as well as the forms within its region, so the transformers can transcribe forms into uses of the macros introduced by the letrec-syntax form.

Note: The forms of a let-syntax and a letrec-syntax form are treated, whether in definition or expression context, as if wrapped in an implicit begin on R6RS mode, it is, then, treated as if wrapped in an implicit let on compatible mode. Thus on compatible mode, it creates a scope.

Macro transformers

In R6RS, it requires '_' '...' as bounded symbols but in Sagittarius these are not bound. And if import clause has rename or prefix these auxiliary syntax are not be renamed or prefixed. This behaivour may be fixed in future.

[R6RS] Each literal must be an identifier. Each rule must have the following form:

(srpattern template)

An srpattern is a restricted form of pattern, namely, a nonempty pattern in one of four parenthesized forms below whose first subform is an identifier or an underscore _. A pattern is an identifier, constant, or one of the following.

An ellipsis is the identifier "..." (three periods).

A template is a pattern variable, an identifier that is not a pattern variable, a pattern datum, or one of the following.

A subtemplate is a template followed by zero or more ellipses.

An instance of syntax-rules evaluates, at macro-expansion time, to a new macro transformer by specifying a sequence of hygienic rewrite rules. A use of a macro whose keyword is associated with a transformer specified by syntax-rulesis matched against the patterns contained in the rules, beginning with the leftmost rule. When a match is found, the macro use is transcribed hygienically according to the template. It is a syntax violation when no match is found.

[R6RS] The ids must be identifiers. The templates must be as for syntax-rules.

When a keyword is bound to a transformer produced by the first form of identifier-syntax, references to the keyword within the scope of the binding are replaced by template.

(define p (cons 4 5))

(define-syntax p.car (identifier-syntax (car p)))

p.car
4

(set! p.car 15)
&syntax exception

The second, more general, form of identifier-syntax permits the transformer to determine what happens when set! is used. In this case, uses of the identifier by itself are replaced by template1, and uses of set! with the identifier are replaced by template2

(define p (cons 4 5))

(define-syntax p.car
  (identifier-syntax
    (_ (car p))
    ((set! _ e) (set-car! p e))))

(set! p.car 15)

p.car
15

p
(15 5)