diff options
| author | Richard M. Stallman | 2005-02-14 10:22:36 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2005-02-14 10:22:36 +0000 |
| commit | 8b480acb35811f6362d97f624fbb17fa10d01dfe (patch) | |
| tree | f038ee5010bd4bdf7badbb77da975609fedcbf1c /lispref/objects.texi | |
| parent | 4182ecfc37b3cf6051321ae7bef4e8a4dce1f94b (diff) | |
| download | emacs-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.texi | 191 |
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 |
| 70 | output generated by the Lisp printer (the function @code{prin1}) for | 70 | output generated by the Lisp printer (the function @code{prin1}) for |
| 71 | that object. The @dfn{read syntax} of an object is the format of the | 71 | that object. Every data type has a unique printed representation. |
| 72 | input accepted by the Lisp reader (the function @code{read}) for that | 72 | The @dfn{read syntax} of an object is the format of the input accepted |
| 73 | object. @xref{Read and Print}. | 73 | by the Lisp reader (the function @code{read}) for that object. This |
| 74 | 74 | is not necessarily unique; many kinds of object have more than one | |
| 75 | Most objects have more than one possible read syntax. Some types of | 75 | syntax. @xref{Read and Print}. |
| 76 | object have no read syntax, since it may not make sense to enter objects | ||
| 77 | of these types directly in a Lisp program. Except for these cases, the | ||
| 78 | printed 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 | ||
| 81 | Lisp, an expression is primarily a Lisp object and only secondarily the | ||
| 82 | text that is the object's read syntax. Often there is no need to | ||
| 83 | emphasize this distinction, but you must keep it in the back of your | ||
| 84 | mind, 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 |
| 88 | syntax---for example, the buffer type has none. Objects of these types | 79 | syntax for the object. However, some types have no read syntax, since |
| 89 | are printed in @dfn{hash notation}: the characters @samp{#<} followed by | 80 | it does not make sense to enter objects of these types as constants in |
| 90 | a descriptive string (typically the type name followed by the name of | 81 | a Lisp program. These objects are printed in @dfn{hash notation}: the |
| 91 | the object), and closed with a matching @samp{>}. Hash notation cannot | 82 | characters @samp{#<} followed by a descriptive string (typically the |
| 92 | be read at all, so the Lisp reader signals the error | 83 | type name followed by the name of the object), and closed with a |
| 93 | @code{invalid-read-syntax} whenever it encounters @samp{#<}. | 84 | matching @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 | ||
| 92 | Hash notation cannot be read at all, so the Lisp reader signals the | ||
| 93 | error @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 | ||
| 97 | Lisp, an expression is primarily a Lisp object and only secondarily the | ||
| 98 | text that is the object's read syntax. Often there is no need to | ||
| 99 | emphasize this distinction, but you must keep it in the back of your | ||
| 100 | mind, 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 |
| 102 | first reads the textual representation of it, producing a Lisp object, | 103 | first reads the textual representation of it, producing a Lisp object, |
| 103 | and then evaluates that object (@pxref{Evaluation}). However, | 104 | and 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 |
| 207 | notation. The precise number of significant figures and the range of | 208 | notation; you can think of a floating point number as a fraction |
| 208 | possible exponents is machine-specific; Emacs always uses the C data | 209 | together with a power of ten. The precise number of significant |
| 209 | type @code{double} to store the value. | 210 | figures and the range of possible exponents is machine-specific; Emacs |
| 211 | uses the C data type @code{double} to store the value, and internally | ||
| 212 | this 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 |
| 212 | a decimal point (with at least one digit following), an exponent, or | 215 | a 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 |
| 478 | name serves as the printed representation of the symbol. In ordinary | 481 | symbol name serves as the printed representation of the symbol. In |
| 479 | use, the name is unique---no two symbols have the same name. | 482 | ordinary Lisp use, with one single obarray (@pxref{Creating Symbols}, |
| 483 | a 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 |
| 482 | property list. Or it may serve only to be distinct from all other Lisp | 486 | property 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 |
| 609 | empty list. @xref{Lists}, for functions that work on lists. Because | 613 | empty list. The empty list is actually the symbol @code{nil}. |
| 610 | most cons cells are used as part of lists, the phrase @dfn{list | 614 | @xref{Lists}, for functions that work on lists. Because most cons |
| 611 | structure} has come to refer to any structure made out of cons cells. | 615 | cells are used as part of lists, the phrase @dfn{list structure} has |
| 612 | 616 | come 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 | ||
| 614 | original Lisp implementation ran on an @w{IBM 704} computer which | ||
| 615 | divided words into two parts, called the ``address'' part and the | ||
| 616 | ``decrement''; @sc{car} was an instruction to extract the contents of | ||
| 617 | the address part of a register, and @sc{cdr} an instruction to extract | ||
| 618 | the contents of the decrement. By contrast, ``cons cells'' are named | ||
| 619 | for the function @code{cons} that creates them, which in turn was named | ||
| 620 | for 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 |
| 629 | consist of a left parenthesis, an arbitrary number of elements, and a | 625 | consist of a left parenthesis, an arbitrary number of elements, and a |
| 630 | right parenthesis. | 626 | right 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).} | ||
| 631 | nil ; @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 |
| 633 | of the list. That is, a cons cell is made for each element. The | 640 | of 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 | |||
| 636 | element in the list. The @sc{cdr} slot of the last cons cell is set to | 643 | element in the list. The @sc{cdr} slot of the last cons cell is set to |
| 637 | hold @code{nil}. | 644 | hold @code{nil}. |
| 638 | 645 | ||
| 646 | The names @sc{car} and @sc{cdr} derive from the history of Lisp. The | ||
| 647 | original Lisp implementation ran on an @w{IBM 704} computer which | ||
| 648 | divided words into two parts, called the ``address'' part and the | ||
| 649 | ``decrement''; @sc{car} was an instruction to extract the contents of | ||
| 650 | the address part of a register, and @sc{cdr} an instruction to extract | ||
| 651 | the contents of the decrement. By contrast, ``cons cells'' are named | ||
| 652 | for the function @code{cons} that creates them, which in turn was named | ||
| 653 | for 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 |
| 642 | shown as pairs of boxes, like dominoes. (The Lisp reader cannot read | 667 | shown as pairs of boxes, like dominoes. (The Lisp reader cannot read |
| 643 | such an illustration; unlike the textual notation, which can be | 668 | such an illustration; unlike the textual notation, which can be |
| @@ -688,19 +713,6 @@ buttercup)}, sketched in a different manner: | |||
| 688 | to the symbol @code{nil}. In other words, @code{nil} is both a symbol | 713 | to the symbol @code{nil}. In other words, @code{nil} is both a symbol |
| 689 | and a list. | 714 | and 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).} | ||
| 696 | nil ; @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)}, |
| 705 | depicted with boxes and arrows: | 717 | depicted 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. | 732 | two-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 |
| 730 | that represents the @sc{car} and @sc{cdr} explicitly. In this syntax, | 777 | represents 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 |
| 732 | the object @var{a}, and whose @sc{cdr} is the object @var{b}. Dotted | 779 | the object @var{a}, and whose @sc{cdr} is the object @var{b}. Dotted |
| 733 | pair notation is therefore more general than list syntax. In the dotted | 780 | pair notation is more general than list syntax because the @sc{cdr} |
| 734 | pair notation, the list @samp{(1 2 3)} is written as @samp{(1 . (2 . (3 | 781 | does not have to be a list. However, it is more cumbersome in cases |
| 735 | . nil)))}. For @code{nil}-terminated lists, you can use either | 782 | where list syntax would work. In dotted pair notation, the list |
| 736 | notation, but list notation is usually clearer and more convenient. | 783 | @samp{(1 2 3)} is written as @samp{(1 . (2 . (3 . nil)))}. For |
| 737 | When 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. | 785 | notation is usually clearer and more convenient. When printing a |
| 786 | list, the dotted pair notation is only used if the @sc{cdr} of a cons | ||
| 787 | cell 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. |
| 741 | This example shows the pair @code{(rose . violet)}: | 790 | This 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 |
| 862 | languages support multidimensional arrays, but they are not essential; | 911 | languages support multidimensional arrays, but they are not essential; |
| 863 | you can get the same effect with an array of arrays.) Each type of | 912 | you can get the same effect with nested one-dimensional arrays.) Each |
| 864 | array has its own read syntax; see the following sections for details. | 913 | type of array has its own read syntax; see the following sections for |
| 914 | details. | ||
| 865 | 915 | ||
| 866 | The array type is contained in the sequence type and | 916 | The array type is contained in the sequence type and |
| 867 | contains the string type, the vector type, the bool-vector type, and the | 917 | contains 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 | ||