Skip to content
Jan Špaček edited this page Apr 12, 2016 · 2 revisions

Module std.core exports the core functions provided by the runtime.

  • Booleans represent yes and no. The only value that makes if and other conditional expressions take the else-branch is false. It also plays the role of nil -- see module std.cons. Any other value will behave as a true value, but there is a canonical true for convenience.

    • true is the true constant (it is defined as (and)).
    • false is the unique false value ((or)).
  • Numbers in Spiral are either signed integers with 31 bits of precision or IEEE floats with 64 bits (doubles).

    • (+ a b), (- a b), (* a b) are the binary standard operations that work on both types of numbers. When given two integers, they return an integer, otherwise they return a float. Note that Spiral does not support variable number of arguments, so these operations always require exactly two arguments.
    • (/ a b) is a floating-point division.
    • (div a b) returns the floor of the quotient floor(a / b) (negative numbers are handled correctly).
    • (mod a b) returns the (positive) remainder of integer division a - b * floor(a / b).

    The comparison operators are also standard:

    • (< a b), (<= a b), (== a b), (/= a b), (>= a b) and (> a b) all work as expected. Integers are considered equal to their float representations. Note that == and /= works only for numbers and panics otherwise.

    There are also two convenience functions:

    • (max a b) returns the maximum of two numbers, and
    • (min a b) returns the minimum.

    Other numeric functions can be found in module std.math.

  • Equivalence predicates work on all kinds of objects:

    • (eqv? a b) returns false when a and b are of different types. For numbers, it checks the equality as defined by == and strings are compared character by character. However, for other objects, eqv? returns true only if they are the exactly same instance (for example, (eqv? (cons 1 2) (cons 1 2)) returns false, while (let ((x (cons 1 2))) (eqv? x x)) returns true).
    • (equal? a b) returns true every time eqv? does, but it additionally checks for structural equality of cons pairs and tuples (true is returned if the tuples have the same sizes and the elements are pairwise equal?). Note that equal? may run out of stack space for deeply nested structures. However, the common case of lists is handled specially and will not cause stack overflow.
  • Helper functions are:

    • (println x) prints the stringified x.
    • (panic msg) panics with the message msg. Panic immediately aborts the program and may be caused by a multitude of issues (like type errors or argument mismatches).