-
Notifications
You must be signed in to change notification settings - Fork 9
Standard Library
When a new interpreter is created, a default set of bindings are available. These comprise the standard library.
Most of the standard library concerns itself with the manipulation of objects of particular types.
(atom? x)
An atom is any object that is not a sequence, that is does not group together other objects in an ordered fashion.
(boolean? x)
(object->boolean x)
(not x)
(number? x)
(fixnum? x)
(flonum? x)
(zero? x)
(even? x)
(odd? x)
The fixnum type in Golisp corresponds to the Go type int
, likewise flonum to float
.
(+ x y)
(- x y)
(* x y)
(/ x y)
Standard arithmetic functions. If both arguments are fixnums, they will return fixnums, except in the case of /
, which will return a fixnum if there is no remainder to the division.
(quotient x y)
(remainder x y)
(modulo x m)
Perform integer division on fixnums. Given x/y = q + r, quotient
returns q and remainder
returns r. modulo
returns x (mod m).
(pair? x)
(list? x)
(proper-list? x)
(improper-list? x)
(null? x)
A proper list is composed of pairs terminated by the empty list, while an improper list is terminated by a pair. When a list is referred to, it is generally as a synonym for a proper list. The function null?
tests if an object is the empty list.
(cons a d)
(car x)
(cdr x)
A pair is, as it sounds, two objects joined together. For historical reasons, the first of these objects is referred to as the car, while the second is known as the cdr. The cons
function creates a new pair.
i.e.
(car (cons 1 2))
=> 1
(cdr (cons 1 2))
=> 2
(caar x) ... (cddddr x)
Shorthands for combining pair access functions are provided. So (cadr x)
is equivalent to (car (cdr x))
, and so on.
(list xs ...)
(zip ls ...)
(fold f i l)
The two fundamental list transformations are zip and fold. The zip
function takes any number of list arguments, and returns a list where the first element is a list containing the first element of each list passed in (in order), the second element the second element of the lists, and so on. The length of the list returned from zip
is equal to the length of the shortest list passed in. The fold
function takes a function, an initial value and a list. It returns the result of iteratively calling the function on each of the elements in the list, accumulating said result as the second argument to the function, beginning with the initial value.
e.g.
(zip '(1 2 3) '(a b c))
=> ((1 a) (2 b) (3 c))
(zip '(1 2 3 4) '(a b c))
=> ((1 a) (2 b) (3 c))
(fold + 0 '(1 2 3))
=> 6
(fold cons '(a b c))
=> (c b a)
Most of the other transformations reduce to fold
and zip
. Note that zip
is self-reversing: (apply zip (zip '(1 2 3) '(a b c)))
returns ((1 2 3) (a b c))
.
(reverse l)
(append ls ...)
(length l)
(filter p l)
(map f ls ...)
(vector? x)
A vector is roughly equivalent to a slice or array.
As well as manipulating objects, functions and syntactic forms for controlling execution are provided.