3 Predefined Functions and Constants
3.1 Booleans
3.2 Lists
value
value
value
value
value
value
> empty - (listof 'a)
'()
> (cons 1 empty) - (listof number)
'(1)
> (first (cons 1 empty)) - number
1
> (rest (cons 1 empty)) - (listof number)
'()
> (define my-list (cons 1 (cons 2 (cons 3 empty)))) > my-list - (listof number)
'(1 2 3)
> (first my-list) - number
1
> (rest my-list) - (listof number)
'(2 3)
> (first (rest my-list)) - number
2
> (define also-my-list (list 1 2 3)) > also-my-list - (listof number)
'(1 2 3)
> (rest also-my-list) - (listof number)
'(2 3)
value
value
value
value
> (define my-list (list 1 2 3)) > (define my-other-list (list 3 4 5)) > (append my-list my-other-list) - (listof number)
'(1 2 3 3 4 5)
> (append my-other-list my-list) - (listof number)
'(3 4 5 1 2 3)
> (map add1 (list 1 2 3)) - (listof number)
'(2 3 4)
> (map to-string (list 1 2 3)) - (listof string)
'("1" "2" "3")
> (filter even? (list 1 2 3 4)) - (listof number)
'(2 4)
> (filter odd? (list 1 2 3 4)) - (listof number)
'(1 3)
> (foldl + 10 (list 1 2 3)) - number
16
> (foldl (lambda (n r) (cons (to-string n) r)) empty (list 1 2 3)) - (listof string)
'("3" "2" "1")
> (foldr (lambda (n r) (cons (to-string n) r)) empty (list 1 2 3)) - (listof string)
'("1" "2" "3")
> (build-list 5 (lambda (v) (* v 10))) - (listof number)
'(0 10 20 30 40)
3.3 Numbers
value
value
value
value
value
value
value
value
value
value
value
value
> (+ 1 2) - number
3
> (- 10 9) - number
1
> (/ 10 5) - number
2
> (modulo 10 3) - number
1
> (remainder 10 3) - number
1
> (min 1 2) - number
1
> (max 1 2) - number
2
> (floor 10.1) - number
10.0
> (ceiling 10.1) - number
11.0
> (ceiling 10.1) - number
11.0
> (add1 10) - number
11
> (sub1 10) - number
9
value
value
value
value
value
value
value
value
> (= 1 1) - boolean
#t
> (> 1 2) - boolean
#f
> (< 1 2) - boolean
#t
> (zero? 1) - boolean
#f
> (odd? 1) - boolean
#t
> (even? 1) - boolean
#f
3.4 Symbols
value
string->symbol : (string -> symbol)
value
symbol->string : (symbol -> string)
> (string->symbol "apple") - symbol
'apple
> (symbol->string 'apple) - string
"apple"
3.5 Strings
value
value
value
string-length : (string -> number)
value
value
string-ref : (string number -> char)
> (string=? "apple" "apple") - boolean
#t
> (string-append "apple" "banana") - string
"applebanana"
> (string-length "apple") - number
5
> (substring "apple" 1 3) - string
"pp"
> (string-ref "apple" 0) - char
#\a
> (to-string 1) - string
"1"
> (to-string 'two) - string
"'two"
> (to-string "three") - string
"\"three\""
> (to-string (list 1 2 3)) - string
"'(1 2 3)"
> (to-string '(1 two "three")) - string
"'(1 two \"three\")"
3.6 Characters
> (char=? #\a #\b) - boolean
#f
value
string->list : (string -> (listof char))
value
list->string : ((listof char) -> string)
> (string->list "apple") - (listof char)
'(#\a #\p #\p #\l #\e)
> (list->string (list #\a #\b #\c)) - string
"abc"
3.7 S-Expressions
A S-expression typically represents program text. For example, placing a ' in from of any plai-typed expression (which is the same as wrapping it with quote) creates an S-expression that contains the identifiers (as symbols), parenthesization (as lists), and other constants as the expression text. Various plai-typed values, including symbols, numbers, and lists, can be coerced to and from S-expression form.
The representation of an S-expression is always the same as some other plai-typed value, so conversion to and from an S-expression is effectively a cast. For example, the s-exp-symbol? function determines whether an S-expression is a symbol; in that case, s-exp->symbol acts the identity function to produce the symbol, while any other value passed to s-exp->symbol raises an exception. The symbol->s-exp function similarly acts as the identity function to view a symbol as an S-expression.
value
value
value
> (s-exp-symbol? `apple) - boolean
#t
> (s-exp->symbol `apple) - symbol
'apple
> (s-exp->symbol '1) - symbol
s-exp->symbol: not a symbol: 1
> (symbol->s-exp 'apple) - s-expression
'apple
value
value
value
> (s-exp-number? '1) - boolean
#t
> (s-exp->number '1) - number
1
> (number->s-exp 1) - s-expression
1
value
value
value
> (s-exp-string? '"apple") - boolean
#t
> (s-exp->string '"apple") - string
"apple"
> (string->s-exp "apple") - s-expression
"apple"
value
value
value
> (s-exp-boolean? '#f) - boolean
#t
> (s-exp->boolean '#f) - boolean
#f
> (boolean->s-exp #f) - s-expression
#f
> (s-exp-boolean? `false) - boolean
#f
> (s-exp-symbol? `false) - boolean
#t
value
value
value
> (s-exp-list? '(1 2 3)) - boolean
#t
> (s-exp-list? '1) - boolean
#f
> (s-exp->list '(1 2 3)) - (listof s-expression)
'(1 2 3)
> (list->s-exp (list '1 '2 '3)) - s-expression
'(1 2 3)
> (list->s-exp (list 1 2 3)) eval:168:0: typecheck failed: s-expression vs. number
sources:
list->s-exp
(list 1 2 3)
1
3.8 Vector
value
make-vector : (number 'a -> (vectorof 'a))
value
vector-ref : ((vectorof 'a) number -> 'a)
value
vector-set! : ((vectorof 'a) number 'a -> void)
value
vector-length : ((vectorof 'a) -> number)
The make-vector function creates a vector of a given size and initializes all vector items to a given value. The vector-ref function accesses the value in a vector slot, and vector-set! changes the value in a slot. The vector-length function reports the number of slots in the vector.
> (define vec (make-vector 10 "apple")) > (vector-length vec) - number
10
> (vector-ref vec 5) - string
"apple"
> (vector-set! vec 5 "banana") - void
> (vector-ref vec 5) - string
"banana"
> (vector-ref vec 6) - string
"apple"
3.9 Boxes
The box function creates a box with an initial value for its slot, unbox accesses the current value in a box’s slot, and set-box! changes the value.
> (define b (box "apple")) > (define b2 b) > (unbox b) - string
"apple"
> (set-box! b "banana") - void
> (unbox b) - string
"banana"
> (unbox b2) - string
"banana"
3.10 Tuples
> (define p (pair 1 "apple")) > p - (number * string)
'#(1 "apple")
> (fst p) - number
1
> (snd p) - string
"apple"
3.11 Optional Values
value
value
value
value
value
(define-type (optionof 'a) [none] [some (v : 'a)])
3.12 Hash Tables
value
value
value
The hash-ref function works on either kind of hash table to find the value for a given key. If the hash table contains a mapping for a given key, hash-ref returns the key’s value wrapped with some. Otherwise, hash-ref returns (none).
> (define m-ht (make-hash (list (pair 1 "apple") (pair 2 "banana")))) > (define i-ht (hash (list (pair 1 "apple") (pair 2 "banana")))) > (hash-ref m-ht 1) - (optionof string)
(some "apple")
> (hash-ref i-ht 1) - (optionof string)
(some "apple")
> (hash-ref m-ht 3) - (optionof string)
(none)
Providing an immutable hash table triggers an exception.
> (define m-ht (make-hash (list (pair 1 "apple") (pair 2 "banana")))) > (hash-ref m-ht 1) - (optionof string)
(some "apple")
> (hash-ref m-ht 3) - (optionof string)
(none)
> (hash-set! m-ht 3 "coconut") - void
> (hash-set! m-ht 1 "Apple") - void
> (hash-ref m-ht 1) - (optionof string)
(some "Apple")
> (hash-ref m-ht 3) - (optionof string)
(some "coconut")
value
value
hash-remove : ((hashof 'a 'b) 'a -> (hashof 'a 'b))
> (define i-ht (hash (list (pair 1 "apple") (pair 2 "banana")))) > (hash-ref i-ht 1) - (optionof string)
(some "apple")
> (define i-ht2 (hash-set (hash-set i-ht 1 "Apple") 3 "coconut")) > (hash-ref i-ht2 1) - (optionof string)
(some "Apple")
> (hash-ref i-ht2 3) - (optionof string)
(some "coconut")
> (hash-ref i-ht 3) - (optionof string)
(none)
> (define i-ht (hash (list (pair 1 "apple") (pair 2 "banana")))) > (hash-keys i-ht) - (listof number)
'(1 2)
3.13 Parameters
value
make-parameter : ('a -> (parameterof 'a))
value
parameter-ref : ((parameterof 'a) -> 'a)
value
parameter-set! : ((parameterof 'a) 'a -> void)
See also parameterize.
3.14 Equality
> (equal? "apple" "apple") - boolean
#t
> (equal? (values 1 'two "three") (values 1 'two "three")) - boolean
#t
3.15 Other Functions
value
read : (-> s-expression)
value
The current continuation is itself represented as a function. Applying a continuation function discards the current continuation and replaces it with the called one, supplying the given value to that continuation.