aboutsummaryrefslogtreecommitdiffstats
path: root/lispref/objects.texi
diff options
context:
space:
mode:
authorRichard M. Stallman2005-02-14 10:22:36 +0000
committerRichard M. Stallman2005-02-14 10:22:36 +0000
commit8b480acb35811f6362d97f624fbb17fa10d01dfe (patch)
treef038ee5010bd4bdf7badbb77da975609fedcbf1c /lispref/objects.texi
parent4182ecfc37b3cf6051321ae7bef4e8a4dce1f94b (diff)
downloademacs-8b480acb35811f6362d97f624fbb17fa10d01dfe.tar.gz
emacs-8b480acb35811f6362d97f624fbb17fa10d01dfe.zip
(Printed Representation): Clarify read syntax vs print.
(Floating Point Type): Explain meaning better. (Symbol Type): Explain uniqueness better. (Cons Cell Type): Explain empty list sooner. CAR and CDR later. List examples sooner. (Box Diagrams): New subnode broken out. Some examples moved from old Lists as Boxes node. (Dotted Pair Notation): Clarify intro. (Array Type): Clarify. (Type Predicates): Add hash-table-p.
Diffstat (limited to 'lispref/objects.texi')
-rw-r--r--lispref/objects.texi191
1 files changed, 122 insertions, 69 deletions
diff --git a/lispref/objects.texi b/lispref/objects.texi
index 43ecb02f09e..f0bef593f29 100644
--- a/lispref/objects.texi
+++ b/lispref/objects.texi
@@ -68,36 +68,37 @@ to use these types can be found in later chapters.
68 68
69 The @dfn{printed representation} of an object is the format of the 69 The @dfn{printed representation} of an object is the format of the
70output generated by the Lisp printer (the function @code{prin1}) for 70output generated by the Lisp printer (the function @code{prin1}) for
71that object. The @dfn{read syntax} of an object is the format of the 71that object. Every data type has a unique printed representation.
72input accepted by the Lisp reader (the function @code{read}) for that 72The @dfn{read syntax} of an object is the format of the input accepted
73object. @xref{Read and Print}. 73by the Lisp reader (the function @code{read}) for that object. This
74 74is not necessarily unique; many kinds of object have more than one
75 Most objects have more than one possible read syntax. Some types of 75syntax. @xref{Read and Print}.
76object have no read syntax, since it may not make sense to enter objects
77of these types directly in a Lisp program. Except for these cases, the
78printed representation of an object is also a read syntax for it.
79
80 In other languages, an expression is text; it has no other form. In
81Lisp, an expression is primarily a Lisp object and only secondarily the
82text that is the object's read syntax. Often there is no need to
83emphasize this distinction, but you must keep it in the back of your
84mind, or you will occasionally be very confused.
85 76
86@cindex hash notation 77@cindex hash notation
87 Every type has a printed representation. Some types have no read 78 In most cases, an object's printed representation is also a read
88syntax---for example, the buffer type has none. Objects of these types 79syntax for the object. However, some types have no read syntax, since
89are printed in @dfn{hash notation}: the characters @samp{#<} followed by 80it does not make sense to enter objects of these types as constants in
90a descriptive string (typically the type name followed by the name of 81a Lisp program. These objects are printed in @dfn{hash notation}: the
91the object), and closed with a matching @samp{>}. Hash notation cannot 82characters @samp{#<} followed by a descriptive string (typically the
92be read at all, so the Lisp reader signals the error 83type name followed by the name of the object), and closed with a
93@code{invalid-read-syntax} whenever it encounters @samp{#<}. 84matching @samp{>}. For example:
94@kindex invalid-read-syntax
95 85
96@example 86@example
97(current-buffer) 87(current-buffer)
98 @result{} #<buffer objects.texi> 88 @result{} #<buffer objects.texi>
99@end example 89@end example
100 90
91@noindent
92Hash notation cannot be read at all, so the Lisp reader signals the
93error @code{invalid-read-syntax} whenever it encounters @samp{#<}.
94@kindex invalid-read-syntax
95
96 In other languages, an expression is text; it has no other form. In
97Lisp, an expression is primarily a Lisp object and only secondarily the
98text that is the object's read syntax. Often there is no need to
99emphasize this distinction, but you must keep it in the back of your
100mind, or you will occasionally be very confused.
101
101 When you evaluate an expression interactively, the Lisp interpreter 102 When you evaluate an expression interactively, the Lisp interpreter
102first reads the textual representation of it, producing a Lisp object, 103first reads the textual representation of it, producing a Lisp object,
103and then evaluates that object (@pxref{Evaluation}). However, 104and then evaluates that object (@pxref{Evaluation}). However,
@@ -204,9 +205,11 @@ leading @samp{+} or a final @samp{.}.
204@subsection Floating Point Type 205@subsection Floating Point Type
205 206
206 Floating point numbers are the computer equivalent of scientific 207 Floating point numbers are the computer equivalent of scientific
207notation. The precise number of significant figures and the range of 208notation; you can think of a floating point number as a fraction
208possible exponents is machine-specific; Emacs always uses the C data 209together with a power of ten. The precise number of significant
209type @code{double} to store the value. 210figures and the range of possible exponents is machine-specific; Emacs
211uses the C data type @code{double} to store the value, and internally
212this records a power of 2 rather than a power of 10.
210 213
211 The printed representation for floating point numbers requires either 214 The printed representation for floating point numbers requires either
212a decimal point (with at least one digit following), an exponent, or 215a decimal point (with at least one digit following), an exponent, or
@@ -474,9 +477,10 @@ following text.)
474@node Symbol Type 477@node Symbol Type
475@subsection Symbol Type 478@subsection Symbol Type
476 479
477 A @dfn{symbol} in GNU Emacs Lisp is an object with a name. The symbol 480 A @dfn{symbol} in GNU Emacs Lisp is an object with a name. The
478name serves as the printed representation of the symbol. In ordinary 481symbol name serves as the printed representation of the symbol. In
479use, the name is unique---no two symbols have the same name. 482ordinary Lisp use, with one single obarray (@pxref{Creating Symbols},
483a symbol's name is unique---no two symbols have the same name.
480 484
481 A symbol can serve as a variable, as a function name, or to hold a 485 A symbol can serve as a variable, as a function name, or to hold a
482property list. Or it may serve only to be distinct from all other Lisp 486property list. Or it may serve only to be distinct from all other Lisp
@@ -606,18 +610,10 @@ Lisp are implicit.
606 610
607 A @dfn{list} is a series of cons cells, linked together so that the 611 A @dfn{list} is a series of cons cells, linked together so that the
608@sc{cdr} slot of each cons cell holds either the next cons cell or the 612@sc{cdr} slot of each cons cell holds either the next cons cell or the
609empty list. @xref{Lists}, for functions that work on lists. Because 613empty list. The empty list is actually the symbol @code{nil}.
610most cons cells are used as part of lists, the phrase @dfn{list 614@xref{Lists}, for functions that work on lists. Because most cons
611structure} has come to refer to any structure made out of cons cells. 615cells are used as part of lists, the phrase @dfn{list structure} has
612 616come to refer to any structure made out of cons cells.
613 The names @sc{car} and @sc{cdr} derive from the history of Lisp. The
614original Lisp implementation ran on an @w{IBM 704} computer which
615divided words into two parts, called the ``address'' part and the
616``decrement''; @sc{car} was an instruction to extract the contents of
617the address part of a register, and @sc{cdr} an instruction to extract
618the contents of the decrement. By contrast, ``cons cells'' are named
619for the function @code{cons} that creates them, which in turn was named
620for its purpose, the construction of cells.
621 617
622@cindex atom 618@cindex atom
623 Because cons cells are so central to Lisp, we also have a word for 619 Because cons cells are so central to Lisp, we also have a word for
@@ -627,7 +623,18 @@ for its purpose, the construction of cells.
627@cindex parenthesis 623@cindex parenthesis
628 The read syntax and printed representation for lists are identical, and 624 The read syntax and printed representation for lists are identical, and
629consist of a left parenthesis, an arbitrary number of elements, and a 625consist of a left parenthesis, an arbitrary number of elements, and a
630right parenthesis. 626right parenthesis. Here are examples of lists:
627
628@example
629(A 2 "A") ; @r{A list of three elements.}
630() ; @r{A list of no elements (the empty list).}
631nil ; @r{A list of no elements (the empty list).}
632("A ()") ; @r{A list of one element: the string @code{"A ()"}.}
633(A ()) ; @r{A list of two elements: @code{A} and the empty list.}
634(A nil) ; @r{Equivalent to the previous.}
635((A B C)) ; @r{A list of one element}
636 ; @r{(which is a list of three elements).}
637@end example
631 638
632 Upon reading, each object inside the parentheses becomes an element 639 Upon reading, each object inside the parentheses becomes an element
633of the list. That is, a cons cell is made for each element. The 640of the list. That is, a cons cell is made for each element. The
@@ -636,8 +643,26 @@ slot refers to the next cons cell of the list, which holds the next
636element in the list. The @sc{cdr} slot of the last cons cell is set to 643element in the list. The @sc{cdr} slot of the last cons cell is set to
637hold @code{nil}. 644hold @code{nil}.
638 645
646 The names @sc{car} and @sc{cdr} derive from the history of Lisp. The
647original Lisp implementation ran on an @w{IBM 704} computer which
648divided words into two parts, called the ``address'' part and the
649``decrement''; @sc{car} was an instruction to extract the contents of
650the address part of a register, and @sc{cdr} an instruction to extract
651the contents of the decrement. By contrast, ``cons cells'' are named
652for the function @code{cons} that creates them, which in turn was named
653for its purpose, the construction of cells.
654
655@menu
656* Box Diagrams:: Drawing pictures of lists.
657* Dotted Pair Notation:: A general syntax for cons cells.
658* Association List Type:: A specially constructed list.
659@end menu
660
661@node Box Diagrams
662@subsubsection Drawing Lists as Box Diagrams
639@cindex box diagrams, for lists 663@cindex box diagrams, for lists
640@cindex diagrams, boxed, for lists 664@cindex diagrams, boxed, for lists
665
641 A list can be illustrated by a diagram in which the cons cells are 666 A list can be illustrated by a diagram in which the cons cells are
642shown as pairs of boxes, like dominoes. (The Lisp reader cannot read 667shown as pairs of boxes, like dominoes. (The Lisp reader cannot read
643such an illustration; unlike the textual notation, which can be 668such an illustration; unlike the textual notation, which can be
@@ -688,19 +713,6 @@ buttercup)}, sketched in a different manner:
688to the symbol @code{nil}. In other words, @code{nil} is both a symbol 713to the symbol @code{nil}. In other words, @code{nil} is both a symbol
689and a list. 714and a list.
690 715
691 Here are examples of lists written in Lisp syntax:
692
693@example
694(A 2 "A") ; @r{A list of three elements.}
695() ; @r{A list of no elements (the empty list).}
696nil ; @r{A list of no elements (the empty list).}
697("A ()") ; @r{A list of one element: the string @code{"A ()"}.}
698(A ()) ; @r{A list of two elements: @code{A} and the empty list.}
699(A nil) ; @r{Equivalent to the previous.}
700((A B C)) ; @r{A list of one element}
701 ; @r{(which is a list of three elements).}
702@end example
703
704 Here is the list @code{(A ())}, or equivalently @code{(A nil)}, 716 Here is the list @code{(A ())}, or equivalently @code{(A nil)},
705depicted with boxes and arrows: 717depicted with boxes and arrows:
706 718
@@ -715,27 +727,64 @@ depicted with boxes and arrows:
715@end group 727@end group
716@end example 728@end example
717 729
718@menu 730 Here is a more complex illustration, showing the three-element list,
719* Dotted Pair Notation:: An alternative syntax for lists. 731@code{((pine needles) oak maple)}, the first element of which is a
720* Association List Type:: A specially constructed list. 732two-element list:
721@end menu 733
734@example
735@group
736 --- --- --- --- --- ---
737 | | |--> | | |--> | | |--> nil
738 --- --- --- --- --- ---
739 | | |
740 | | |
741 | --> oak --> maple
742 |
743 | --- --- --- ---
744 --> | | |--> | | |--> nil
745 --- --- --- ---
746 | |
747 | |
748 --> pine --> needles
749@end group
750@end example
751
752 The same list represented in the first box notation looks like this:
753
754@example
755@group
756 -------------- -------------- --------------
757| car | cdr | | car | cdr | | car | cdr |
758| o | o------->| oak | o------->| maple | nil |
759| | | | | | | | | |
760 -- | --------- -------------- --------------
761 |
762 |
763 | -------------- ----------------
764 | | car | cdr | | car | cdr |
765 ------>| pine | o------->| needles | nil |
766 | | | | | |
767 -------------- ----------------
768@end group
769@end example
722 770
723@node Dotted Pair Notation 771@node Dotted Pair Notation
724@comment node-name, next, previous, up
725@subsubsection Dotted Pair Notation 772@subsubsection Dotted Pair Notation
726@cindex dotted pair notation 773@cindex dotted pair notation
727@cindex @samp{.} in lists 774@cindex @samp{.} in lists
728 775
729 @dfn{Dotted pair notation} is an alternative syntax for cons cells 776 @dfn{Dotted pair notation} is a general syntax for cons cells that
730that represents the @sc{car} and @sc{cdr} explicitly. In this syntax, 777represents the @sc{car} and @sc{cdr} explicitly. In this syntax,
731@code{(@var{a} .@: @var{b})} stands for a cons cell whose @sc{car} is 778@code{(@var{a} .@: @var{b})} stands for a cons cell whose @sc{car} is
732the object @var{a}, and whose @sc{cdr} is the object @var{b}. Dotted 779the object @var{a}, and whose @sc{cdr} is the object @var{b}. Dotted
733pair notation is therefore more general than list syntax. In the dotted 780pair notation is more general than list syntax because the @sc{cdr}
734pair notation, the list @samp{(1 2 3)} is written as @samp{(1 . (2 . (3 781does not have to be a list. However, it is more cumbersome in cases
735. nil)))}. For @code{nil}-terminated lists, you can use either 782where list syntax would work. In dotted pair notation, the list
736notation, but list notation is usually clearer and more convenient. 783@samp{(1 2 3)} is written as @samp{(1 . (2 . (3 . nil)))}. For
737When printing a list, the dotted pair notation is only used if the 784@code{nil}-terminated lists, you can use either notation, but list
738@sc{cdr} of a cons cell is not a list. 785notation is usually clearer and more convenient. When printing a
786list, the dotted pair notation is only used if the @sc{cdr} of a cons
787cell is not a list.
739 788
740 Here's an example using boxes to illustrate dotted pair notation. 789 Here's an example using boxes to illustrate dotted pair notation.
741This example shows the pair @code{(rose . violet)}: 790This example shows the pair @code{(rose . violet)}:
@@ -860,8 +909,9 @@ Once an array is created, its length is fixed.
860 909
861 All Emacs Lisp arrays are one-dimensional. (Most other programming 910 All Emacs Lisp arrays are one-dimensional. (Most other programming
862languages support multidimensional arrays, but they are not essential; 911languages support multidimensional arrays, but they are not essential;
863you can get the same effect with an array of arrays.) Each type of 912you can get the same effect with nested one-dimensional arrays.) Each
864array has its own read syntax; see the following sections for details. 913type of array has its own read syntax; see the following sections for
914details.
865 915
866 The array type is contained in the sequence type and 916 The array type is contained in the sequence type and
867contains the string type, the vector type, the bool-vector type, and the 917contains the string type, the vector type, the bool-vector type, and the
@@ -1661,6 +1711,9 @@ with references to further information.
1661@item functionp 1711@item functionp
1662@xref{Functions, functionp}. 1712@xref{Functions, functionp}.
1663 1713
1714@item hash-table-p
1715@xref{Other Hash, hash-table-p}.
1716
1664@item integer-or-marker-p 1717@item integer-or-marker-p
1665@xref{Predicates on Markers, integer-or-marker-p}. 1718@xref{Predicates on Markers, integer-or-marker-p}.
1666 1719