-
Notifications
You must be signed in to change notification settings - Fork 0
module std.core
Jan Špaček edited this page Apr 12, 2016
·
2 revisions
Module std.core
exports the core functions provided by the runtime.
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]].
-
true
is the true constant (it is defined as(and)
). -
false
is the unique false value.
Spiral provides built-in signed integers with 31 bits of precision and IEEE
floats with 64 bits (double
s).
-
(+ 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 quotientfloor(a / b)
(negative numbers are handled correctly). -
(mod a b)
returns the (positive) remainder of integer divisiona - 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.
There are two standard predicates that work on all kinds of objects.
-
(eqv? a b)
returnsfalse
whena
andb
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?
returnstrue
only if they are the exactly same instance (for example,(eqv? (cons 1 2) (cons 1 2))
returnsfalse
, while(let ((x (cons 1 2))) (eqv? x x))
returnstrue
). -
(equal? a b)
returnstrue
every timeeqv?
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 pairwiseequal?
). Note thatequal?
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.
-
(println x)
prints the stringifiedx
. -
(panic msg)
panics with the messagemsg
. Panic immediately aborts the program and may be caused by a multitude of issues (like type errors or argument mismatchs).