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
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
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 flit 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 flit 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 flit 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:147:0: typecheck failed: S-Exp vs. Number
sources:
list->s-exp
1
(list 1 2 3)
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 Tuples
For an introduction, see the tutorial section Tuples and Options.
4.9 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.10 Equality
> (equal? "apple" "apple") - Boolean
#t
> (equal? (values 1 'two "three") (values 1 'two "three")) - Boolean
#t
4.11 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