guide edits

svn: r6342
This commit is contained in:
Matthew Flatt 2007-05-27 00:00:11 +00:00
parent 48e93852f3
commit b6391ebfdd
11 changed files with 294 additions and 25 deletions

View File

@ -183,7 +183,7 @@
(define/override (render-table t part ht)
`((table ((cellspacing "0") ,@(case (table-style t)
[(boxed) '((width "100%") (bgcolor "lightgray"))]
[(boxed) '((width "100%") (bgcolor "#E8E8FF"))]
[(centered) '((align "center"))]
[(at-right) '((align "right"))]
[(at-left) '((align "left"))]

View File

@ -248,6 +248,18 @@
p-color)
(set! src-col (+ src-col 1))
(hash-table-put! col-map src-col dest-col))]
[(hash-table? (syntax-e c))
(advance c init-line!)
(let ([equal-table? (hash-table? (syntax-e c) 'equal)])
(out (if equal-table?
"#hash"
"#hasheq")
value-color)
(set! src-col (+ src-col 5 (if equal-table? 2 0)))
(hash-table-put! col-map src-col dest-col)
((loop init-line! +inf.0)
(syntax-ize (hash-table-map (syntax-e c) cons)
(syntax-column c))))]
[else
(advance c init-line!)
(let-values ([(s it? sub?)

View File

@ -18,11 +18,12 @@
}
.refcontent {
background-color: beige;
background-color: #F5F5DC;
display: block;
position: relative;
width: 10em;
font-size: 85%;
border: 0.5em solid #F5F5DC;
}
h1,h2,h3,h4,h5,h6 {

View File

@ -0,0 +1,25 @@
#reader(lib "docreader.ss" "scribble")
@require[(lib "manual.ss" "scribble")]
@require[(lib "eval.ss" "scribble")]
@require["guide-utils.ss"]
@title{Booleans}
Scheme has two distinguished constants to represent boolean values:
@scheme[#t] for true and @scheme[#f] for false. Uppercase
@schemevalfont{#T} and @schemevalfont{#F} are parsed as the same
values, but the lowercase forms are preferred.
The @scheme[boolean?] procedure recognizes the two boolean
constants. In the result of a test expression for @scheme[if],
@scheme[cond], @scheme[and], @scheme[or], etc., however, any value
other than @scheme[#f] counts as true.
@examples[
(= 2 (+ 1 1))
(boolean? #t)
(boolean? #f)
(boolean? "no")
(if "no" 1 0)
]

View File

@ -3,7 +3,7 @@
@require[(lib "eval.ss" "scribble")]
@require["guide-utils.ss"]
@title[#:tag "datatypes" #:style 'toc]{Built-In and Programmer-Defined Datatypes}
@title[#:tag "datatypes" #:style 'toc]{Built-In Datatypes}
The @seclink["to-scheme"]{little Scheme section} introduced some of
Scheme's built-in datatype: numbers, booleans, strings, lists, and
@ -14,28 +14,12 @@ class-based object system to @secref["classes"].
@local-table-of-contents[]
@section{Booleans}
Scheme has two distinguished constants to represent boolean values:
@scheme[#t] for true and @scheme[#f] for false. Uppercase
@schemevalfont{#T} and @schemevalfont{#F} are parsed as the same
values, but the lowercase forms are preferred.
The @scheme[boolean?] procedure recognizes the two boolean
constants. In the result of a test expression for @scheme[if],
@scheme[cond], @scheme[and], @scheme[or], etc., however, any value
other than @scheme[#f] counts as true.
@examples[
(= 2 (+ 1 1))
(boolean? #t)
(boolean? #f)
(boolean? "no")
(if "no" 1 0)
]
@include-section["booleans.scrbl"]
@include-section["numbers.scrbl"]
@include-section["chars.scrbl"]
@include-section["char-strings.scrbl"]
@include-section["byte-strings.scrbl"]
@include-section["symbols.scrbl"]
@include-section["pairs.scrbl"]
@include-section["vectors.scrbl"]
@include-section["hash-tables.scrbl"]

View File

@ -24,6 +24,8 @@ precise details to @|MzScheme| and other reference manuals.
@include-section["data.scrbl"]
@section{Programmer-Defined Datatypes}
@; ----------------------------------------------------------------------
@section[#:tag "scheme-forms"]{Programs and Expressions}

View File

@ -0,0 +1,69 @@
#reader(lib "docreader.ss" "scribble")
@require[(lib "manual.ss" "scribble")]
@require[(lib "eval.ss" "scribble")]
@require["guide-utils.ss"]
@title[#:tag "hash-tables"]{Hash Tables}
A @defterm{hash table} implements a maping from keys to values, where
both keys can values can be arbitrary Scheme values, and access and
update to the tabel are normally constant-time operations. Keys are
compared using @scheme[equal?] or @scheme[eq?], depending on whether
the hash table is created with @scheme['equal] or @scheme['eq].
@examples[
(define ht (make-hash-table 'equal))
(hash-table-put! ht "apple" '(red round))
(hash-table-put! ht "banana" '(yellow long))
(hash-table-get ht "apple")
(hash-table-get ht "coconut")
(hash-table-get ht "coconut" "not there")
]
A literal hash table can be written as an expression by using
@schemefont{#hash} (for an @scheme[equal?]-based table) or
@schemefont{#hasheq} (for an @scheme[eq?]-based table). A parenthesized
sequence must immediately follow @schemefont{#hash} or
@schemefont{#hasheq}, where each element is a sequence is a dotted
key--value pair. Literal hash tables are immutable.
@examples[
(define ht #hash(("apple" . (red round))
("banana" . (yellow long))))
(hash-table-get ht "apple")
]
@refdetails["mz:parse-hashtable"]{the syntax of hash table literals}
A hash table can optionally retain its keys @defterm{weakly}, so the
mapping is retained only so long as the key is retained elsewhere.
@examples[
(define ht (make-hash-table 'weak))
(hash-table-put! ht (gensym) "can you see me?")
(collect-garbage)
(eval:alts (hash-table-count ht) 0)
]
Beware that a weak hash table retains its values strongly, as long as
the corresponding key is accessible. This creates a catch-22
dependency in the case that a value refers back to its key, so that
the mapping is retained permanently. To break the cycle, map the key
to an @seclink["ephemerons"]{ephemeron} that pair the value with its
key (in addition to the implicit pairing of the hash table).
@examples[
(define ht (make-hash-table 'weak))
(let ([g (gensym)])
(hash-table-put! ht g (list g)))
(collect-garbage)
(eval:alts (hash-table-count ht) 1)
]
@interaction[
(define ht (make-hash-table 'weak))
(let ([g (gensym)])
(hash-table-put! ht g (make-ephemeron g (list g))))
(collect-garbage)
(eval:alts (hash-table-count ht) 0)
]

View File

@ -0,0 +1,90 @@
#reader(lib "docreader.ss" "scribble")
@require[(lib "manual.ss" "scribble")]
@require[(lib "eval.ss" "scribble")]
@require["guide-utils.ss"]
@interaction-eval[(require (lib "list.ss"))]
@interaction-eval[(define mutable-cons cons)]
@title{Pairs and Lists}
A @defterm{pair} joins two arbitrary values. The @scheme[cons]
procedure constructs pairs, and the @scheme[car] and @scheme[cdr]
procedures extract the first and second elements of the pair,
respectively. The @scheme[pair?] predicate recogizes pairs.
Some pairs print by wrapping parentheses around the printed forms of
the two pair elements, putting a @litchar{.} between them.
@examples[
(cons 1 2)
(cons (cons 1 2) 3)
(car (cons 1 2))
(cdr (cons 1 2))
(pair? (cons 1 2))
]
A @defterm{list} is a combination of pairs that creates a linked
list. More precisely, a list is either the empty list @scheme[null],
or it is a pair whose first element is a list element and whose second
element is a list. The @scheme[list?] predicate recognizes lists. The
@scheme[null?] predicate recognizes the empty list.
A list prints as a pair of parentheses wrapped around the list
elements.
@examples[
null
(cons 0 (cons 1 (cons 2 null)))
(list? null)
(list? (cons 1 (cons 2 null)))
(list? (cons 1 2))
]
An expression with @litchar{'} followed by the printed form of a pair
or list produces a pair or list constant.
@examples[
'()
'(1 . 2)
'(1 2 3)
]
A pair can be mutable or immutable. Most pairs are immutable (contrary
to Lisp tradition), and @scheme[pair?] and @scheme[list?] recognize
immutable pairs and lists, only. The @scheme[mutable-cons] procedure
creates a mutable pair, which works with @scheme[set-car!] and
@scheme[set-cdr!], as well as @scheme[car] and @scheme[cdr].
@examples[
(define p (mutable-cons 1 2))
p
(eval:alts (pair? p) #f)
(set-car! p 0)
p
]
Among the most important predefined proecdures on lists are those that
iterate through the lists elements:
@interaction[
(map (lambda (i) (/ 1 i))
'(1 2 3))
(andmap (lambda (i) (i . < . 3))
'(1 2 3))
(ormap (lambda (i) (i . < . 3))
'(1 2 3))
(filter (lambda (i) (i . < . 3))
'(1 2 3))
(foldl (lambda (v i) (+ v i))
10
'(1 2 3))
(for-each (lambda (i) (display i))
'(1 2 3))
(member "Keys"
'("Florida" "Keys" "U.S.A."))
(assoc 'where
'((when "3:30") (where "Florida") (who "Mickey")))
]
@refdetails["mz:pairs"]{pairs and lists}

View File

@ -0,0 +1,86 @@
#reader(lib "docreader.ss" "scribble")
@require[(lib "manual.ss" "scribble")]
@require[(lib "eval.ss" "scribble")]
@require["guide-utils.ss"]
@title{Symbols}
A @defterm{symbol} is an atomic value that prints like an identifier.
An expression that starts with @litchar{'} and continues with an
identifier produces a symbol value.
@examples[
'a
(symbol? 'a)
]
For any sequence of characters, exactly one corresponding symbol is
@defterm{interned}; the @scheme[string->symbol] procedure or
@scheme[read]ing a syntactic identifier produces an interned
symbol. Since interned symbols can be cheaply compared with
@scheme[eq?] (and thus @scheme[eqv?] or @scheme[equal?]), they serves
as a convenient values to use for tags and enumerations.
Symbols are case-sensitive. By using a @schemefont{#ci} suffix or in
other ways, the reader can be made to case-fold character sequences to
arrive at a symbol, but the reader preserves case by default.
@examples[
(eq? 'a 'a)
(eq? 'a (string->symbol "a"))
(eq? 'a 'b)
(eq? 'a 'A)
(eval:alts #, @elem{@schemefont{#ci}@schemevalfont{'A}} #ci'A)
]
Any string (i.e., any character sequence) can be supplied to
@scheme[string->symbol] to obtain the corresponding symbol. For reader
input, any character can appear directly in an identifier, except for
whitespace and the following special characters:
@t{
@hspace[2] @litchar{(} @litchar{)} @litchar{[} @litchar{]}
@litchar["{"] @litchar["}"]
@litchar{"} @litchar{,} @litchar{'} @litchar{`}
@litchar{;} @litchar{#} @litchar["|"] @litchar["\\"]
}
Actually, @litchar{#} is disallowed only at the beginning of a symbol,
and then only if not followed by @litchar{%}; otherwise, @litchar{#} is
allowed, too. Also, @litchar{.} by itself is not a symbol.
Whitespace or special characters can be included in an identifier by
quoting them with @litchar["|"] or @litchar["\\"]. These quoting
mechanisms are used in the printed form of identifiers that contain
special characters or that might otherwise look like numbers.
@examples[
(string->symbol "one, two")
(string->symbol "6")
]
@refdetails["mz:parse-symbol"]{the syntax of symbols}
The @scheme[display] form of a symbol is the same as the corresponding
string.
@examples[
(display 'Apple)
(display '|6|)
]
The @scheme[gensym] and @scheme[string->uninterned-symbol] procedures
generate fresh @defterm{uninterned} symbols that are not equal
(according to @scheme[eq?]) to any previously interned or uninterned
symbol. Uninterned symbols are useful as fresh tags that cannot be
confused with any other value.
@examples[
(define s (gensym))
(eval:alts s 'g42)
(eval:alts (eq? s 'g42) #f)
(eq? 'a (string->uninterned-symbol "a"))
]
@refdetails["mz:symbols"]{symbols}

View File

@ -168,7 +168,7 @@ special characters
@hspace[2] @litchar{(} @litchar{)} @litchar{[} @litchar{]}
@litchar["{"] @litchar["}"]
@litchar{"} @litchar{,} @litchar{'} @litchar{`}
@litchar{;} @litchar{#}
@litchar{;} @litchar{#} @litchar["|"] @litchar["\\"]
}
and except for the sequences of characters that make number constants,

View File

@ -90,7 +90,7 @@ ephemeron key (see @secref["ephemeron"]).
@section[#:tag "keywords"]{Keywords}
@; ----------------------------------------------------------------------
@section[#:tag "pairs"]{Pairs and Lists}
@section[#:tag "mz:pairs"]{Pairs and Lists}
@defproc[(cons [a any/c] [d any/c]) pair?]{Returns a pair whose first
element is @scheme[a] and second element is @scheme[d].}