Preview
Sign inSign up
Forms and builtinsLists

Forms and builtins

Lists

30 builtins. Every example below ran on the evaluator, and the result shown is what it returned.

cons

arity 2
(cons a b)

A pair of the two arguments.

(list (cons 1 '(2 3)) (cons 1 2))

=>((1 2 3) (1 . 2))

car

arity 1
(car pair)

First element of a pair.

(car '(1 2 3))

=>1

cdr

arity 1
(cdr pair)

Rest of a pair.

(cdr '(1 2 3))

=>(2 3)

caar

arity 1
(caar x)

(car (car x))

(caar '((1 2) 3))

=>1

cadr

arity 1
(cadr x)

(car (cdr x))

(cadr '(1 2 3))

=>2

cdar

arity 1
(cdar x)

(cdr (car x))

(cdar '((1 2) 3))

=>(2)

cddr

arity 1
(cddr x)

(cdr (cdr x))

(cddr '(1 2 3))

=>(3)

caddr

arity 1
(caddr x)

(car (cdr (cdr x)))

(caddr '(1 2 3))

=>3

list

arity 0+
(list x ...)

A list of the arguments.

(list 1 'two "three")

=>(1 two "three")

length

arity 1
(length list)

Number of elements in a list.

(length '(a b c))

=>3

append

arity 0+
(append list ... [tail])

Joins lists; the last argument may be any value.

(append '(1 2) '(3) '(4 5))

=>(1 2 3 4 5)

reverse

arity 1
(reverse list)

The list in reverse order.

(reverse '(1 2 3))

=>(3 2 1)

list-ref

arity 2
(list-ref list k)

Element at index k (from 0).

(list-ref '(a b c) 1)

=>b

list-tail

arity 2
(list-tail list k)

The list without its first k elements.

(list-tail '(a b c d) 2)

=>(c d)

null?

arity 1
(null? x)

Is the argument the empty list?

(list (null? '()) (null? '(1)))

=>(#t #f)

pair?

arity 1
(pair? x)

Is the argument a pair?

(list (pair? '(1)) (pair? '()))

=>(#t #f)

list?

arity 1
(list? x)

Is the argument a proper list?

(list (list? '(1 2)) (list? (cons 1 2)))

=>(#t #f)

map

arity 2+
(map f list ...)

Applies a procedure to each element; with several lists, stops at the shortest.

(map + '(1 2 3) '(10 20 30))

=>(11 22 33)

for-each

arity 2+
(for-each f list ...)

Like map, for side effects.

(for-each (lambda (x) (display x) (newline)) '(1 2 3))

stdout1 2 3

filter

arity 2
(filter pred list)

Elements for which the predicate is true.

(filter odd? '(1 2 3 4 5))

=>(1 3 5)

reduce

arity 3
(reduce f initial list)

(reduce f initial list): folds with (f element acc), starting from the first element; initial if the list is empty.

(reduce max 0 '(3 9 2))

=>9

fold-left

arity 3
(fold-left f init list)

(fold-left f init list): folds from the left with (f acc element).

(fold-left (lambda (acc x) (cons x acc)) '() '(1 2 3))

=>(3 2 1)

fold-right

arity 3
(fold-right f init list)

(fold-right f init list): folds from the right with (f element acc).

(fold-right cons '() '(1 2 3))

=>(1 2 3)

apply

arity 2+
(apply f arg ... list)

(apply f arg ... list): calls f with the args followed by the list's elements.

(apply + 1 2 '(3 4))

=>10

assoc

arity 2
(assoc key alist)

First pair in an association list whose car is equal? to the key, or #f.

(assoc "b" '(("a" . 1) ("b" . 2)))

=>("b" . 2)

assq

arity 2
(assq key alist)

Like assoc.

(assq 'b '((a 1) (b 2)))

=>(b 2)

assv

arity 2
(assv key alist)

Like assoc.

(assv 2 '((1 . one) (2 . two)))

=>(2 . two)

member

arity 2
(member x list)

The sublist starting at the first element equal? to x, or #f.

(member 3 '(1 2 3 4))

=>(3 4)

memq

arity 2
(memq x list)

Like member.

(memq 'c '(a b c d))

=>(c d)

memv

arity 2
(memv x list)

Like member.

(memv 5 '(1 2))

=>#f

esc