4 Predefined Functions and Constants
4.1 Booleans
> (not #t) - Boolean
#f
4.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)
4.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
4.4 Symbols
value
string->symbol : (String -> Symbol)
value
symbol->string : (Symbol -> String)
> (string->symbol "apple") - Symbol
'apple
> (symbol->string 'apple) - String
"apple"
4.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\")"
4.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"
4.7 S-Expressions
For an introduction, see the tutorial section S-Expressions.
A S-expression typically represents program text. For example, placing a ' in from of any plait 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 plait values, including symbols, numbers, and lists, can be coerced to and from S-expression form.
The representation of an S-expression always reuses some other plait value, so conversion to and from an S-expression is a kind cast. For example, the s-exp-symbol? function determines whether an S-expression embeds an immediate symbol; in that case, s-exp->symbol extracts the symbol, while any other value passed to s-exp->symbol raises an exception. The symbol->s-exp function wraps a symbol as an S-expression.
For interoperability of S-expressions with untyped Racket programs, see s-exp-content and s-exp.
value
s-exp-symbol? : (S-Exp -> Boolean)
value
s-exp->symbol : (S-Exp -> Symbol)
value
symbol->s-exp : (Symbol -> S-Exp)
> (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-Exp
`apple
value
s-exp-number? : (S-Exp -> Boolean)
value
s-exp->number : (S-Exp -> Number)
value
number->s-exp : (Number -> S-Exp)
> (s-exp-number? `1) - Boolean
#t
> (s-exp->number `1) - Number
1
> (number->s-exp 1) - S-Exp
`1
value
s-exp-string? : (S-Exp -> Boolean)
value
s-exp->string : (S-Exp -> String)
value
string->s-exp : (String -> S-Exp)
> (s-exp-string? `"apple") - Boolean
#t
> (s-exp->string `"apple") - String
"apple"
> (string->s-exp "apple") - S-Exp
`"apple"
value
s-exp-boolean? : (S-Exp -> Boolean)
value
s-exp->boolean : (S-Exp -> Boolean)
value
boolean->s-exp : (Boolean -> S-Exp)
> (s-exp-boolean? `#f) - Boolean
#t
> (s-exp->boolean `#f) - Boolean
#f
> (boolean->s-exp #f) - S-Exp
`#f
> (s-exp-boolean? `false) - Boolean
#f
> (s-exp-symbol? `false) - Boolean
#t
value
s-exp-list? : (S-Exp -> Boolean)
value
s-exp->list : (S-Exp -> (Listof S-Exp))
value
list->s-exp : ((Listof S-Exp) -> S-Exp)
> (s-exp-list? `(1 2 3)) - Boolean
#t
> (s-exp-list? `1) - Boolean
#f
> (s-exp->list `(1 2 3)) - (Listof S-Exp)
(list `1 `2 `3)
> (list->s-exp (list `1 `2 `3)) - S-Exp
`(1 2 3)
> (list->s-exp (list 1 2 3)) eval:167:0: typecheck failed: S-Exp vs. Number
sources:
list->s-exp
(list 1 2 3)
1
value
s-exp-match? : (S-Exp S-Exp -> Boolean)
For an introduction, see the tutorial section S-Expression Matching.
Compares the first S-expression, a pattern, to the second S-expression, a target.
To a first approximation, s-exp-match? is just equal? on the two S-expressions. Unlike equal?, however, certain symbols in the pattern and can match various S-expressions within the target.
For example, `NUMBER within a pattern matches any number in corresponding position within the target:
> (s-exp-match? `(+ NUMBER NUMBER) `(+ 1 10)) - Boolean
#t
> (s-exp-match? `(+ NUMBER NUMBER) `(+ 1 x)) - Boolean
#f
> (s-exp-match? `(+ NUMBER NUMBER) `(- 1 10)) - Boolean
#f
The following symbol S-expressions are treated specially within the pattern:
`NUMBER —
matches any number S-expression `STRING —
matches any string S-expression `SYMBOL —
matches any symbol S-expression `ANY —
matches any S-expression `... —
within a list S-expression, matches any number of repetitions of the preceding S-expression within the list; only one `... can appear as an immediate element of a pattern list, and `... is not allowed within a pattern outside of a list or as the first element of a list
Any other symbol in a pattern matches only itself in the target. For example, `+ matches only `+.
> (s-exp-match? `NUMBER `10) - Boolean
#t
> (s-exp-match? `NUMBER `a) - Boolean
#f
> (s-exp-match? `SYMBOL `a) - Boolean
#t
> (s-exp-match? `SYMBOL `"a") - Boolean
#f
> (s-exp-match? `STRING `"a") - Boolean
#t
> (s-exp-match? `STRING `("a")) - Boolean
#f
> (s-exp-match? `ANY `("a")) - Boolean
#t
> (s-exp-match? `ANY `10) - Boolean
#t
> (s-exp-match? `any `10) - Boolean
#f
> (s-exp-match? `any `any) - Boolean
#t
> (s-exp-match? `(SYMBOL) `(a)) - Boolean
#t
> (s-exp-match? `(SYMBOL) `(a b)) - Boolean
#f
> (s-exp-match? `(SYMBOL SYMBOL) `(a b)) - Boolean
#t
> (s-exp-match? `((SYMBOL) SYMBOL) `((a) b)) - Boolean
#t
> (s-exp-match? `((SYMBOL) NUMBER) `((a) b)) - Boolean
#f
> (s-exp-match? `((SYMBOL) NUMBER ((STRING))) `((a) 5 (("c")))) - Boolean
#t
> (s-exp-match? `(lambda (SYMBOL) ANY) `(lambda (x) x)) - Boolean
#t
> (s-exp-match? `(lambda (SYMBOL) ANY) `(function (x) x)) - Boolean
#f
> (s-exp-match? `(SYMBOL ...) `(a b)) - Boolean
#t
> (s-exp-match? `(a ...) `(a b)) - Boolean
#f
> (s-exp-match? `(a ...) `(a a)) - Boolean
#t
> (s-exp-match? `(a ...) `()) - Boolean
#t
> (s-exp-match? `(a ... b) `()) - Boolean
#f
> (s-exp-match? `(a ... b) `(b)) - Boolean
#t
> (s-exp-match? `(a ... b) `(a a a b)) - Boolean
#t
> (s-exp-match? `((a ...) b ...) `((a a a) b b b b)) - Boolean
#t
> (s-exp-match? `((a ...) b ...) `((a a a) b c b b)) - Boolean
#f
4.8 Vector
For an introduction, see the tutorial section State.
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"
4.9 Boxes
For an introduction, see the tutorial section State.
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 bx (box "apple")) > (define bx2 bx) > (unbox bx) - String
"apple"
> (set-box! bx "banana") - Void
> (unbox bx) - String
"banana"
> (unbox bx2) - String
"banana"
4.10 Tuples
For an introduction, see the tutorial section Tuples and Options.
> (define p (pair 1 "apple")) > p - (Number * String)
(values 1 "apple")
> (fst p) - Number
1
> (snd p) - String
"apple"
4.11 Optional Values
For an introduction, see the tutorial section Tuples and Options.
value
value
value
value
value
(define-type (Optionof 'a) (none) (some [v : 'a]))
4.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)
4.13 Parameters
value
make-parameter : ('a -> (Parameterof 'a))
value
parameter-ref : ((Parameterof 'a) -> 'a)
value
parameter-set! : ((Parameterof 'a) 'a -> Void)
See also parameterize.
4.14 Equality
> (equal? "apple" "apple") - Boolean
#t
> (equal? (values 1 'two "three") (values 1 'two "three")) - Boolean
#t
4.15 Other Functions
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.
value
s-exp-content : no type
value
s-exp : no type
value
tuple-content : no type
value
tuple : no type