Racket is dynamically typed: a value carries its type, and a variable can hold any value. Typed Racket adds a checker on top (see Typed Racket). This page lists the kinds of value the language has, how each is written, and how values are compared.
The values
| Type | Literal | Notes |
|---|---|---|
| Boolean | #t #f #true #false | Only #f counts as false: 0, "", '() and (void) are all true. boolean?, not, and, or. true and false are the same values under library names. |
| Number | 42 3/4 2.5 1+2i | A tower: exact integers (unbounded, with fixnums as the small fast ones), exact rationals, flonums (double precision), exact and inexact complex numbers. See below. |
| Character | #\a #\space #\u3BB | A Unicode scalar value (a code point except the surrogates). char?, char->integer, integer->char. |
| String | "text" | A sequence of characters. A literal is immutable; make-string, string, string-copy and string-append make mutable ones. string->immutable-string converts. Strings are compared by string=? or equal?, not eq?. |
| Byte string | #"ab\0" | A sequence of bytes (0 to 255). Used for binary data and for file paths. |
| Symbol | 'apple '|two words| | An interned name: two symbols that print the same are eq?. string->symbol, symbol->string, string->uninterned-symbol and gensym make others. |
| Keyword | #:key | Written only after ' or in the argument position of a function; the reader makes it from #: and a symbol. See Functions. |
| Pair, list | '(1 . 2) '(1 2 3) | A pair is cons of two values, immutable. A list is '() or a pair whose second part is a list. null and empty name '(). list? tests for a proper list, pair? for a pair. |
| Mutable pair | (mcons 1 2) | Written {1 . 2} by the printer. Separate from pairs, in racket/base as mcons, mcar, mcdr, set-mcar!. |
| Vector | #(1 2 3) | A fixed-length, indexed sequence. A literal vector is immutable; make-vector and vector make mutable ones. flvector and fxvector hold only flonums or fixnums. |
| Box | #&5 | A single mutable (or immutable, if literal) cell. box, unbox, set-box!. |
| Hash table | #hash((a . 1)) | A mapping, keyed by equal?, eqv?, eq? or equal-always?; mutable (make-hash) or immutable (hash, hash-set); a mutable one may hold its keys weakly (make-weak-hash). The literal is always immutable. |
| Set | (no literal) | racket/set: (set 1 2 3), seteq, mutable-set; with set-add, set-member?, set-union and the rest. Built on hash tables. |
| Structure | #s(point 1 2) | An instance of a type made by struct; the literal form is only for prefab structures. See Structs. |
| Procedure | (no literal) | lambda and define make them; a structure can also be applied (prop:procedure). procedure?, procedure-arity. |
| Void | (void) | The value that when with a false test, set! and many printing functions return. The REPL prints nothing for it. void?. |
| End of file | eof | What a read function returns at the end of a port. eof-object?. |
| Regular expression | #rx"a+" #px"\\d" | See Strings and regexps. |
| Path | (no literal) | An operating-system path: (build-path "a" "b"), (string->path "x"); path-string? accepts a string or path. |
| Syntax object | #'(f x) | A datum with source locations and lexical context, the input and output of a macro. See Macros. |
| Promise, parameter, thread, port, semaphore, channel, place, future, custodian, namespace | (no literals) | Runtime objects; see Concurrency and I/O and ports. |
Numbers
Every number is exact or inexact. An exact number is an integer, a ratio of integers, or a complex number made of those. An inexact number is a flonum (an IEEE 754 double) or a complex number of flonums. Adding, subtracting, multiplying and dividing exact numbers always gives an exact result, so (/ 1 3) is the ratio 1/3; an inexact number anywhere makes the result inexact. exact->inexact and inexact->exact convert; exact? and inexact? test.
- A fixnum is an exact integer that fits in 61 or 63 bits on a 64-bit platform (30 or 31 on a 32-bit one), depending on the variant of Racket; using it does not allocate. Two fixnums that are
=areeq?.racket/fixnumandracket/flonumhold operations on fixnums and flonums only (fx+,fl+, …). - An extflonum is an extended-precision flonum, written with
tas the exponent mark (1.0t0). It is a different type from a flonum, and arithmetic on it works only where(extflonum-available?)is true; where it is false the literals still read and print as data. As far as I know Racket CS is such a platform (this is from memory: the downloaded documentation only says the answer depends on the platform). - A single-flonum (exponent mark
fors) exists only where(single-flonum-available?)is true, which is the Racket BC variant; elsewhere those literals read as double-precision flonums. - Special inexact values:
+inf.0,-inf.0and+nan.0(and the.fand.tkinds).(= +nan.0 +nan.0)is false, but(equal? +nan.0 +nan.0)is true. - Functions:
+ - * /,quotient remainder modulo,abs min max,floor ceiling round truncate,sqrt expt exp log sin cos tan atan,exact-roundand its relatives,number->string string->number,random, bitwise operations (bitwise-and,arithmetic-shift),racket/math(pi,sqr,sgn,nan?,infinite?,exact-round).
Equality
| Predicate | True when |
|---|---|
eq? | the two values are the same object. Fixnums, characters, symbols, keywords, #t, #f and '() are always eq? when equal; do not rely on it for other numbers or for strings |
eqv? | the same, and also two numbers of the same exactness that are numerically the same (and two equal characters) |
equal? | the two values are eqv?, or are compound values (pairs, vectors, strings, hash tables, boxes, prefab and transparent structures) whose parts are equal? |
equal-always? | like equal?, but values must also stay equal after any mutation: two mutable strings are not equal-always? even with the same characters. (Added in version 8.5.0.3.) |
=, string=?, char=?, symbol=? | the type-specific comparisons; = compares numerically ((= 1 1.0) is true but (equal? 1 1.0) is false) |
equal? on structures compares fields only for transparent and prefab types and for those that declare an equal? method (prop:equal+hash or gen:equal+hash); an opaque structure is equal? only to itself.
Lists, vectors and hash tables at a glance
#lang racket
(define xs (list 1 2 3))
(cons 0 xs) ; '(0 1 2 3)
(map add1 xs) ; '(2 3 4)
(vector-ref #(a b c) 1) ; 'b
(define h (hash 'a 1 'b 2)) ; immutable, keys compared with equal?
(hash-ref h 'a) ; 1
(hash-set h 'c 3) ; a new table
(define m (make-hash))
(hash-set! m "k" 1) ; mutates
(hash-ref m "k" #f) ; 1; #f when the key is missing
(for/list ([(k v) (in-hash h)]) (list k v))
The standard library adds more containers and iteration forms: racket/list, racket/vector, racket/dict (a generic interface to hash tables, association lists and vectors), racket/set, racket/stream and racket/sequence. A sequence is anything a for loop can walk: a list, vector, string, hash table, range, port, stream, or a structure with the prop:sequence property. See Control and loops and The standard library.
Truthiness and the void
A test position (if, cond, when, and, or) treats every value but #f as true. Functions that search return #f for not found, which is also why (and x y) and (or x y) return a value of x or y rather than a boolean. Where a function can return #f as a real result, it usually takes a failure argument (hash-ref takes a default or a thunk).
Not described
Known, not yet covered
- Reference: only the types that most programs meet are described. Stencil vectors, impersonators and chaperones, weak boxes and ephemerons, tree lists (
racket/treelist), the rest of the Racket Reference's data chapter, and the full procedure lists of each type were not read. - Reference: the table of literals gives the reader's forms; how each value is printed back (the
write,displayandprintconventions and theprint-…parameters) is not described.
See also
- Lexical structure – how each literal is read
- Structs – defining new types
- The standard library – the library tour