#
Chapter 14
`etc.ss`: Useful Procedures and Syntax

`(boolean=?``` `bool1 bool2``)` PROCEDURE

Returns `#t` if `bool1` and `bool2` are both `#t` or both `#f`, and returns `#f` otherwise. If either `bool1` or `bool2` is not a Boolean, the `exn:application:type` exception is raised.

`(build-list``` `n f``)` PROCEDURE

Creates a list of `n` elements by applying `f` to the integers from `0` to `n` `-` `1` in order, where `n` is a non-negative integer. The `i`th element of the resulting list is `(`f` (- `i` 1))`.

`(build-string``` `n f``)` PROCEDURE

Creates a string of length `n` by applying `f` to the integers from `0` to `n` `-` `1` in order, where `n` is a non-negative integer and `f` returns a character for the `n` invocations. The `i`th character of the resulting string is `(`f` (- `i` 1))`.

`(build-vector``` `n f``)` PROCEDURE

Creates a vector of `n` elements by applying `f` to the integers from `0` to `n` `-` `1` in order, where `n` is a non-negative integer. The `i`th element of the resulting vector is `(`f` (- `i` 1))`.

`(compose``` `f g``)` PROCEDURE

Returns a procedure that takes `x` and returns `(`call-with-values` (lambda () (g x)) f)`.

`(`define-syntax-set` ``(`identifier `···``)` defn `···``)` SYNTAX

This form is similar to `define-syntaxes`, but instead of a single body expression, a sequence of definitions follows the sequence of defined identifiers. For each `identifier`, the `defn`s should include a definition for ``identifier`/proc`. The value for ``identifier`/proc` is used as the (expansion-time) value for `identifier`.

The `define-syntax-set` form is especially useful for defining a set of syntax transformers that share helper functions.

Example:

    (define-syntax-set (let-current-continuation let-current-escape-continuation)
      (define (mk call-id)
         (lambda (stx)
           (syntax-case stx ()
             [(_ id body1 body ...)
              (with-syntax ([call call-id])
                (syntax (call (lambda (id) body1 body ...))))])))
      (define let-current-continuation/proc (mk (quote-syntax call/cc)))
      (define let-current-escape-continuation/proc (mk (quote-syntax call/ec))))

`(`evcase` `key-expr `(`value-expr body-expr `···``)` `···`1`)` SYNTAX

The `evcase` form is similar to `case`, except that expressions are provided in each clause instead of a sequence of data. After `key-expr` is evaluated, each `value-expr` is evaluated until a value is found that is `eqv?` to the key value; when a matching value is found, the corresponding `body-expr`s are evaluated and the value(s) for the last is the result of the entire `evcase` expression.

A `value-expr` can be the special identifier `else`. This identifier is recognized as in `case` (see section 2.3 in PLT MzScheme: Language Manual).

`false` BOOLEAN

Boolean false.

`(identity``` `v``)` PROCEDURE

Returns `v`.

`(`let+` `clause body-expr `···`1`)` SYNTAX

A new binding construct that specifies scoping on a per-binding basis instead of a per-expression basis. It helps eliminate rightward-drift in programs. It looks similar to `let`, except each clause has an additional keyword tag before the binding variables.

Each `clause` has one of the following forms:

- `(val target expr)` binds `target` non-recursively to `expr`.

- `(rec target expr)` binds `target` recursively to `expr`.

- `(vals (target expr) `···`)` the `target`s are bound to the `expr`s. The environment of the `expr`s is the environment active before this clause.

- `(recs (variable expr) `···`)` the `targets`s are bound to the `expr`s. The environment of the `expr`s includes all of the `targets`s.

- `(_ expr `···`)` evaluates the `expr`s without binding any variables.

The clauses bind left-to-right. Each `target` above can either be an identifier or `(values variable `···`)`. In the latter case, multiple values returned by the corresponding expression are bound to the multiple variables.

Examples:

    (let+ ([val (values x y) (values 1 2)])
       (list x y)) ; => '(1 2)

    (let ([x 1])
       (let+ ([val x 3]
              [val y x])
          y)) ; => 3

`(`local` ``(`definition `···``)` body-expr `···`1`)` SYNTAX

This is a binding form similar to `letrec`, except that each `definition` is a `define-values` expression (after partial macro expansion). The `body-expr`s are evaluated in the lexical scope of these definitions.

`(loop-until``` `start done? next f``)` PROCEDURE

Repeatedly invokes the `f` procedure until the `done?` procedure returns `#t`. The procedure is best described by its implementation:

    (define loop-until
      (lambda (start done? next f)
        (let loop ([i start])
          (unless (done? i)
            (f i)
            (loop (next i))))))

`(namespace-defined?``` `symbol``)` PROCEDURE

Returns `#t` if `namespace-variable-value` would return a value for `symbol`, `#f` otherwise. See section 8.2 in PLT MzScheme: Language Manual for further information.

`(`nand` `expr `···``)` SYNTAX

Returns `(`not` (and expr `···`))`.

`(`nor` `expr `···``)` SYNTAX

Returns `(`not` (or expr `···`))`.

`(`opt-lambda` `formals body-expr `···`1`)` SYNTAX

The `opt-lambda` form is like `lambda`, except that default values are assigned to arguments (C++-style). Default values are defined in the `formals` list by replacing each `variable` by `[`variable` `default-value-expression`]`. If an variable has a default value expression, then all (non-aggregate) variables after it must have default value expressions. A final aggregate variable can be used as in `lambda`, but it cannot be given a default value. Each default value expression is evaluated only if it is needed. The environment of each default value expression includes the preceding arguments.

For example:

    (define f
      (opt-lambda (a [b (add1 a)] . c)
         ...))

In the example, `f` is a procedure which takes at least one argument. If a second argument is specified, it is the value of `b`, otherwise `b` is `(`add1` a)`. If more than two arguments are specified, then the extra arguments are placed in a new list that is the value of `c`.

`(`recur` `name bindings body-expr `···`1`)` SYNTAX

This is equivalent to a named `let`: `(let name bindings body-expr `···`1)`.

`(`rec` `name value-expr`)` SYNTAX

This is equivalent to a `letrec` expression that returns its binding: `(letrec ((name value-expr)) name)`.

`(symbol=?``` `symbol1 symbol2``)` PROCEDURE

Returns `#t` if `symbol1` and `symbol2` are equivalent (as determined by `eq?`), `#f` otherwise. If either `symbol1` or `symbol2` is not a symbol, the `exn:application:type` exception is raised.

`(`this-expression-source-directory`)` SYNTAX

Expands to a string that names the directory of the file containing the source expression. The source expression's file is determined through source location information associated with the syntax if it is present. Otherwise, `current-load-relative-directory` is used if it is not `#f`, and `current-directory` is used if all else fails.

`true` BOOLEAN

Boolean true.