1 This is ../info/lispref.info, produced by makeinfo version 4.0 from
4 INFO-DIR-SECTION XEmacs Editor
6 * Lispref: (lispref). XEmacs Lisp Reference Manual.
11 GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
12 Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
13 Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
14 XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
15 GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
16 Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
17 Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
18 Reference Manual (for 19.15 and 20.1, 20.2, 20.3) v3.2, April, May,
19 November 1997 XEmacs Lisp Reference Manual (for 21.0) v3.3, April 1998
21 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
22 Foundation, Inc. Copyright (C) 1994, 1995 Sun Microsystems, Inc.
23 Copyright (C) 1995, 1996 Ben Wing.
25 Permission is granted to make and distribute verbatim copies of this
26 manual provided the copyright notice and this permission notice are
27 preserved on all copies.
29 Permission is granted to copy and distribute modified versions of
30 this manual under the conditions for verbatim copying, provided that the
31 entire resulting derived work is distributed under the terms of a
32 permission notice identical to this one.
34 Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that this permission notice may be stated in a
37 translation approved by the Foundation.
39 Permission is granted to copy and distribute modified versions of
40 this manual under the conditions for verbatim copying, provided also
41 that the section entitled "GNU General Public License" is included
42 exactly as in the original, and provided that the entire resulting
43 derived work is distributed under the terms of a permission notice
44 identical to this one.
46 Permission is granted to copy and distribute translations of this
47 manual into another language, under the above conditions for modified
48 versions, except that the section entitled "GNU General Public License"
49 may be included in a translation approved by the Free Software
50 Foundation instead of in the original English.
53 File: lispref.info, Node: Specifier Type, Next: Font Instance Type, Prev: Glyph Type, Up: Window-System Types
61 File: lispref.info, Node: Font Instance Type, Next: Color Instance Type, Prev: Specifier Type, Up: Window-System Types
69 File: lispref.info, Node: Color Instance Type, Next: Image Instance Type, Prev: Font Instance Type, Up: Window-System Types
77 File: lispref.info, Node: Image Instance Type, Next: Toolbar Button Type, Prev: Color Instance Type, Up: Window-System Types
85 File: lispref.info, Node: Toolbar Button Type, Next: Subwindow Type, Prev: Image Instance Type, Up: Window-System Types
93 File: lispref.info, Node: Subwindow Type, Next: X Resource Type, Prev: Toolbar Button Type, Up: Window-System Types
101 File: lispref.info, Node: X Resource Type, Prev: Subwindow Type, Up: Window-System Types
109 File: lispref.info, Node: Type Predicates, Next: Equality Predicates, Prev: Window-System Types, Up: Lisp Data Types
114 The XEmacs Lisp interpreter itself does not perform type checking on
115 the actual arguments passed to functions when they are called. It could
116 not do so, since function arguments in Lisp do not have declared data
117 types, as they do in other programming languages. It is therefore up to
118 the individual function to test whether each actual argument belongs to
119 a type that the function can use.
121 All built-in functions do check the types of their actual arguments
122 when appropriate, and signal a `wrong-type-argument' error if an
123 argument is of the wrong type. For example, here is what happens if you
124 pass an argument to `+' that it cannot handle:
127 error--> Wrong type argument: integer-or-marker-p, a
129 If you want your program to handle different types differently, you
130 must do explicit type checking. The most common way to check the type
131 of an object is to call a "type predicate" function. Emacs has a type
132 predicate for each type, as well as some predicates for combinations of
135 A type predicate function takes one argument; it returns `t' if the
136 argument belongs to the appropriate type, and `nil' otherwise.
137 Following a general Lisp convention for predicate functions, most type
138 predicates' names end with `p'.
140 Here is an example which uses the predicates `listp' to check for a
141 list and `symbolp' to check for a symbol.
145 ;; If X is a symbol, put it on LIST.
146 (setq list (cons x list)))
148 ;; If X is a list, add its elements to LIST.
149 (setq list (append x list)))
151 ;; We only handle symbols and lists.
152 (error "Invalid argument %s in add-on" x))))
154 Here is a table of predefined type predicates, in alphabetical order,
155 with references to further information.
158 *Note annotationp: Annotation Primitives.
161 *Note arrayp: Array Functions.
164 *Note atom: List-related Predicates.
167 *Note bit-vector-p: Bit Vector Functions.
170 *Note bitp: Bit Vector Functions.
172 `boolean-specifier-p'
173 *Note boolean-specifier-p: Specifier Types.
176 *Note buffer-glyph-p: Glyph Types.
179 *Note buffer-live-p: Killing Buffers.
182 *Note bufferp: Buffer Basics.
185 *Note button-event-p: Event Predicates.
187 `button-press-event-p'
188 *Note button-press-event-p: Event Predicates.
190 `button-release-event-p'
191 *Note button-release-event-p: Event Predicates.
194 *Note case-table-p: Case Tables.
197 *Note char-int-p: Character Codes.
200 *Note char-or-char-int-p: Character Codes.
203 *Note char-or-string-p: Predicates for Strings.
206 *Note char-table-p: Char Tables.
209 *Note characterp: Predicates for Characters.
212 *Note color-instance-p: Colors.
214 `color-pixmap-image-instance-p'
215 *Note color-pixmap-image-instance-p: Image Instance Types.
218 *Note color-specifier-p: Specifier Types.
221 *Note commandp: Interactive Call.
223 `compiled-function-p'
224 *Note compiled-function-p: Compiled-Function Type.
227 *Note console-live-p: Connecting to a Console or Device.
230 *Note consolep: Consoles and Devices.
233 *Note consp: List-related Predicates.
236 *Note database-live-p: Connecting to a Database.
239 *Note databasep: Databases.
242 *Note device-live-p: Connecting to a Console or Device.
245 *Note device-or-frame-p: Basic Device Functions.
248 *Note devicep: Consoles and Devices.
251 *Note eval-event-p: Event Predicates.
254 *Note event-live-p: Event Predicates.
257 *Note eventp: Events.
260 *Note extent-live-p: Creating and Modifying Extents.
263 *Note extentp: Extents.
265 `face-boolean-specifier-p'
266 *Note face-boolean-specifier-p: Specifier Types.
269 *Note facep: Basic Face Functions.
272 *Note floatp: Predicates on Numbers.
275 *Note font-instance-p: Fonts.
278 *Note font-specifier-p: Specifier Types.
281 *Note frame-live-p: Deleting Frames.
284 *Note framep: Frames.
289 `generic-specifier-p'
290 *Note generic-specifier-p: Specifier Types.
293 *Note glyphp: Glyphs.
296 *Note hash-table-p: Hash Tables.
299 *Note icon-glyph-p: Glyph Types.
302 *Note image-instance-p: Images.
305 *Note image-specifier-p: Specifier Types.
307 `integer-char-or-marker-p'
308 *Note integer-char-or-marker-p: Predicates on Markers.
311 *Note integer-or-char-p: Predicates for Characters.
313 `integer-or-marker-p'
314 *Note integer-or-marker-p: Predicates on Markers.
316 `integer-specifier-p'
317 *Note integer-specifier-p: Specifier Types.
320 *Note integerp: Predicates on Numbers.
326 *Note key-press-event-p: Event Predicates.
329 *Note keymapp: Creating Keymaps.
335 *Note listp: List-related Predicates.
338 *Note markerp: Predicates on Markers.
341 *Note misc-user-event-p: Event Predicates.
343 `mono-pixmap-image-instance-p'
344 *Note mono-pixmap-image-instance-p: Image Instance Types.
347 *Note motion-event-p: Event Predicates.
350 *Note mouse-event-p: Event Predicates.
353 *Note natnum-specifier-p: Specifier Types.
356 *Note natnump: Predicates on Numbers.
359 *Note nlistp: List-related Predicates.
361 `nothing-image-instance-p'
362 *Note nothing-image-instance-p: Image Instance Types.
364 `number-char-or-marker-p'
365 *Note number-char-or-marker-p: Predicates on Markers.
368 *Note number-or-marker-p: Predicates on Markers.
371 *Note numberp: Predicates on Numbers.
374 *Note pointer-glyph-p: Glyph Types.
376 `pointer-image-instance-p'
377 *Note pointer-image-instance-p: Image Instance Types.
380 *Note process-event-p: Event Predicates.
383 *Note processp: Processes.
386 *Note range-table-p: Range Tables.
392 *Note sequencep: Sequence Functions.
395 *Note specifierp: Specifiers.
398 *Note stringp: Predicates for Strings.
401 *Note subrp: Function Cells.
403 `subwindow-image-instance-p'
404 *Note subwindow-image-instance-p: Image Instance Types.
407 *Note subwindowp: Subwindows.
410 *Note symbolp: Symbols.
413 *Note syntax-table-p: Syntax Tables.
415 `text-image-instance-p'
416 *Note text-image-instance-p: Image Instance Types.
419 *Note timeout-event-p: Event Predicates.
422 *Note toolbar-button-p: Toolbar.
424 `toolbar-specifier-p'
425 *Note toolbar-specifier-p: Toolbar.
428 *Note user-variable-p: Defining Variables.
431 *Note vectorp: Vectors.
434 *Note weak-list-p: Weak Lists.
436 `window-configuration-p'
437 *Note window-configuration-p: Window Configurations.
440 *Note window-live-p: Deleting Windows.
443 *Note windowp: Basic Windows.
445 The most general way to check the type of an object is to call the
446 function `type-of'. Recall that each object belongs to one and only
447 one primitive type; `type-of' tells you which one (*note Lisp Data
448 Types::). But `type-of' knows nothing about non-primitive types. In
449 most cases, it is more convenient to use type predicates than `type-of'.
451 - Function: type-of object
452 This function returns a symbol naming the primitive type of
453 OBJECT. The value is one of `bit-vector', `buffer', `char-table',
454 `character', `charset', `coding-system', `cons', `color-instance',
455 `compiled-function', `console', `database', `device', `event',
456 `extent', `face', `float', `font-instance', `frame', `glyph',
457 `hash-table', `image-instance', `integer', `keymap', `marker',
458 `process', `range-table', `specifier', `string', `subr',
459 `subwindow', `symbol', `toolbar-button', `tooltalk-message',
460 `tooltalk-pattern', `vector', `weak-list', `window',
461 `window-configuration', or `x-resource'.
467 (type-of '()) ; `()' is `nil'.
473 File: lispref.info, Node: Equality Predicates, Prev: Type Predicates, Up: Lisp Data Types
478 Here we describe two functions that test for equality between any two
479 objects. Other functions test equality between objects of specific
480 types, e.g., strings. For these predicates, see the appropriate chapter
481 describing the data type.
483 - Function: eq object1 object2
484 This function returns `t' if OBJECT1 and OBJECT2 are the same
485 object, `nil' otherwise. The "same object" means that a change in
486 one will be reflected by the same change in the other.
488 `eq' returns `t' if OBJECT1 and OBJECT2 are integers with the same
489 value. Also, since symbol names are normally unique, if the
490 arguments are symbols with the same name, they are `eq'. For
491 other types (e.g., lists, vectors, strings), two arguments with
492 the same contents or elements are not necessarily `eq' to each
493 other: they are `eq' only if they are the same object.
495 (The `make-symbol' function returns an uninterned symbol that is
496 not interned in the standard `obarray'. When uninterned symbols
497 are in use, symbol names are no longer unique. Distinct symbols
498 with the same name are not `eq'. *Note Creating Symbols::.)
500 NOTE: Under XEmacs 19, characters are really just integers, and
501 thus characters and integers are `eq'. Under XEmacs 20, it was
502 necessary to preserve remnants of this in function such as `old-eq'
503 in order to maintain byte-code compatibility. Byte code compiled
504 under any Emacs 19 will automatically have calls to `eq' mapped to
505 `old-eq' when executed under XEmacs 20.
516 (eq '(1 (2 (3))) '(1 (2 (3))))
519 (setq foo '(1 (2 (3))))
523 (eq foo '(1 (2 (3))))
526 (eq [(1 2) 3] [(1 2) 3])
529 (eq (point-marker) (point-marker))
533 - Function: old-eq obj1 obj2
534 This function exists under XEmacs 20 and is exactly like `eq'
535 except that it suffers from the char-int confoundance disease. In
536 other words, it returns `t' if given a character and the
537 equivalent integer, even though the objects are of different types!
538 You should _not_ ever call this function explicitly in your code.
539 However, be aware that all calls to `eq' in byte code compiled
540 under version 19 map to `old-eq' in XEmacs 20. (Likewise for
541 `old-equal', `old-memq', `old-member', `old-assq' and
544 ;; Remember, this does not apply under XEmacs 19.
550 => t ; Eek, we've been infected.
552 => nil ; We are still healthy.
554 - Function: equal object1 object2
555 This function returns `t' if OBJECT1 and OBJECT2 have equal
556 components, `nil' otherwise. Whereas `eq' tests if its arguments
557 are the same object, `equal' looks inside nonidentical arguments
558 to see if their elements are the same. So, if two objects are
559 `eq', they are `equal', but the converse is not always true.
567 (equal "asdf" "asdf")
572 (equal '(1 (2 (3))) '(1 (2 (3))))
574 (eq '(1 (2 (3))) '(1 (2 (3))))
577 (equal [(1 2) 3] [(1 2) 3])
579 (eq [(1 2) 3] [(1 2) 3])
582 (equal (point-marker) (point-marker))
585 (eq (point-marker) (point-marker))
588 Comparison of strings is case-sensitive.
590 Note that in FSF GNU Emacs, comparison of strings takes into
591 account their text properties, and you have to use `string-equal'
592 if you want only the strings themselves compared. This difference
593 does not exist in XEmacs; `equal' and `string-equal' always return
594 the same value on the same strings.
596 (equal "asdf" "ASDF")
599 Two distinct buffers are never `equal', even if their contents are
602 The test for equality is implemented recursively, and circular lists
603 may therefore cause infinite recursion (leading to an error).
606 File: lispref.info, Node: Numbers, Next: Strings and Characters, Prev: Lisp Data Types, Up: Top
611 XEmacs supports two numeric data types: "integers" and "floating
612 point numbers". Integers are whole numbers such as -3, 0, #b0111,
613 #xFEED, #o744. Their values are exact. The number prefixes `#b',
614 `#o', and `#x' are supported to represent numbers in binary, octal, and
615 hexadecimal notation (or radix). Floating point numbers are numbers
616 with fractional parts, such as -4.5, 0.0, or 2.71828. They can also be
617 expressed in exponential notation: 1.5e2 equals 150; in this example,
618 `e2' stands for ten to the second power, and is multiplied by 1.5.
619 Floating point values are not exact; they have a fixed, limited amount
624 * Integer Basics:: Representation and range of integers.
625 * Float Basics:: Representation and range of floating point.
626 * Predicates on Numbers:: Testing for numbers.
627 * Comparison of Numbers:: Equality and inequality predicates.
628 * Numeric Conversions:: Converting float to integer and vice versa.
629 * Arithmetic Operations:: How to add, subtract, multiply and divide.
630 * Rounding Operations:: Explicitly rounding floating point numbers.
631 * Bitwise Operations:: Logical and, or, not, shifting.
632 * Math Functions:: Trig, exponential and logarithmic functions.
633 * Random Numbers:: Obtaining random integers, predictable or not.
636 File: lispref.info, Node: Integer Basics, Next: Float Basics, Up: Numbers
641 The range of values for an integer depends on the machine. The
642 minimum range is -134217728 to 134217727 (28 bits; i.e., -2**27 to
643 2**27 - 1), but some machines may provide a wider range. Many examples
644 in this chapter assume an integer has 28 bits.
646 The Lisp reader reads an integer as a sequence of digits with
647 optional initial sign and optional final period.
651 +1 ; Also the integer 1.
653 268435457 ; Also the integer 1, due to overflow.
657 To understand how various functions work on integers, especially the
658 bitwise operators (*note Bitwise Operations::), it is often helpful to
659 view the numbers in their binary form.
661 In 28-bit binary, the decimal integer 5 looks like this:
663 0000 0000 0000 0000 0000 0000 0101
665 (We have inserted spaces between groups of 4 bits, and two spaces
666 between groups of 8 bits, to make the binary integer easier to read.)
668 The integer -1 looks like this:
670 1111 1111 1111 1111 1111 1111 1111
672 -1 is represented as 28 ones. (This is called "two's complement"
675 The negative integer, -5, is creating by subtracting 4 from -1. In
676 binary, the decimal integer 4 is 100. Consequently, -5 looks like this:
678 1111 1111 1111 1111 1111 1111 1011
680 In this implementation, the largest 28-bit binary integer is the
681 decimal integer 134,217,727. In binary, it looks like this:
683 0111 1111 1111 1111 1111 1111 1111
685 Since the arithmetic functions do not check whether integers go
686 outside their range, when you add 1 to 134,217,727, the value is the
687 negative integer -134,217,728:
691 => 1000 0000 0000 0000 0000 0000 0000
693 Many of the following functions accept markers for arguments as well
694 as integers. (*Note Markers::.) More precisely, the actual arguments
695 to such functions may be either integers or markers, which is why we
696 often give these arguments the name INT-OR-MARKER. When the argument
697 value is a marker, its position value is used and its buffer is ignored.
700 File: lispref.info, Node: Float Basics, Next: Predicates on Numbers, Prev: Integer Basics, Up: Numbers
702 Floating Point Basics
703 =====================
705 XEmacs supports floating point numbers. The precise range of
706 floating point numbers is machine-specific; it is the same as the range
707 of the C data type `double' on the machine in question.
709 The printed representation for floating point numbers requires either
710 a decimal point (with at least one digit following), an exponent, or
711 both. For example, `1500.0', `15e2', `15.0e2', `1.5e3', and `.15e4'
712 are five ways of writing a floating point number whose value is 1500.
713 They are all equivalent. You can also use a minus sign to write
714 negative floating point numbers, as in `-1.0'.
716 Most modern computers support the IEEE floating point standard, which
717 provides for positive infinity and negative infinity as floating point
718 values. It also provides for a class of values called NaN or
719 "not-a-number"; numerical functions return such values in cases where
720 there is no correct answer. For example, `(sqrt -1.0)' returns a NaN.
721 For practical purposes, there's no significant difference between
722 different NaN values in XEmacs Lisp, and there's no rule for precisely
723 which NaN value should be used in a particular case, so this manual
724 doesn't try to distinguish them. XEmacs Lisp has no read syntax for
725 NaNs or infinities; perhaps we should create a syntax in the future.
727 You can use `logb' to extract the binary exponent of a floating
728 point number (or estimate the logarithm of an integer):
730 - Function: logb number
731 This function returns the binary exponent of NUMBER. More
732 precisely, the value is the logarithm of NUMBER base 2, rounded
736 File: lispref.info, Node: Predicates on Numbers, Next: Comparison of Numbers, Prev: Float Basics, Up: Numbers
738 Type Predicates for Numbers
739 ===========================
741 The functions in this section test whether the argument is a number
742 or whether it is a certain sort of number. The functions `integerp'
743 and `floatp' can take any type of Lisp object as argument (the
744 predicates would not be of much use otherwise); but the `zerop'
745 predicate requires a number as its argument. See also
746 `integer-or-marker-p', `integer-char-or-marker-p', `number-or-marker-p'
747 and `number-char-or-marker-p', in *Note Predicates on Markers::.
749 - Function: floatp object
750 This predicate tests whether its argument is a floating point
751 number and returns `t' if so, `nil' otherwise.
753 `floatp' does not exist in Emacs versions 18 and earlier.
755 - Function: integerp object
756 This predicate tests whether its argument is an integer, and
757 returns `t' if so, `nil' otherwise.
759 - Function: numberp object
760 This predicate tests whether its argument is a number (either
761 integer or floating point), and returns `t' if so, `nil' otherwise.
763 - Function: natnump object
764 The `natnump' predicate (whose name comes from the phrase
765 "natural-number-p") tests to see whether its argument is a
766 nonnegative integer, and returns `t' if so, `nil' otherwise. 0 is
767 considered non-negative.
769 - Function: zerop number
770 This predicate tests whether its argument is zero, and returns `t'
771 if so, `nil' otherwise. The argument must be a number.
773 These two forms are equivalent: `(zerop x)' == `(= x 0)'.
776 File: lispref.info, Node: Comparison of Numbers, Next: Numeric Conversions, Prev: Predicates on Numbers, Up: Numbers
778 Comparison of Numbers
779 =====================
781 To test numbers for numerical equality, you should normally use `=',
782 not `eq'. There can be many distinct floating point number objects
783 with the same numeric value. If you use `eq' to compare them, then you
784 test whether two values are the same _object_. By contrast, `='
785 compares only the numeric values of the objects.
787 At present, each integer value has a unique Lisp object in XEmacs
788 Lisp. Therefore, `eq' is equivalent to `=' where integers are
789 concerned. It is sometimes convenient to use `eq' for comparing an
790 unknown value with an integer, because `eq' does not report an error if
791 the unknown value is not a number--it accepts arguments of any type.
792 By contrast, `=' signals an error if the arguments are not numbers or
793 markers. However, it is a good idea to use `=' if you can, even for
794 comparing integers, just in case we change the representation of
795 integers in a future XEmacs version.
797 There is another wrinkle: because floating point arithmetic is not
798 exact, it is often a bad idea to check for equality of two floating
799 point values. Usually it is better to test for approximate equality.
800 Here's a function to do this:
802 (defconst fuzz-factor 1.0e-6)
803 (defun approx-equal (x y)
804 (or (and (= x 0) (= y 0))
806 (max (abs x) (abs y)))
809 Common Lisp note: Comparing numbers in Common Lisp always requires
810 `=' because Common Lisp implements multi-word integers, and two
811 distinct integer objects can have the same numeric value. XEmacs
812 Lisp can have just one integer object for any given value because
813 it has a limited range of integer values.
815 In addition to numbers, all of the following functions also accept
816 characters and markers as arguments, and treat them as their number
819 - Function: = number &rest more-numbers
820 This function returns `t' if all of its arguments are numerically
821 equal, `nil' otherwise.
832 - Function: /= number &rest more-numbers
833 This function returns `t' if no two arguments are numerically
834 equal, `nil' otherwise.
843 - Function: < number &rest more-numbers
844 This function returns `t' if the sequence of its arguments is
845 monotonically increasing, `nil' otherwise.
854 - Function: <= number &rest more-numbers
855 This function returns `t' if the sequence of its arguments is
856 monotonically nondecreasing, `nil' otherwise.
865 - Function: > number &rest more-numbers
866 This function returns `t' if the sequence of its arguments is
867 monotonically decreasing, `nil' otherwise.
869 - Function: >= number &rest more-numbers
870 This function returns `t' if the sequence of its arguments is
871 monotonically nonincreasing, `nil' otherwise.
873 - Function: max number &rest more-numbers
874 This function returns the largest of its arguments.
883 - Function: min number &rest more-numbers
884 This function returns the smallest of its arguments.
890 File: lispref.info, Node: Numeric Conversions, Next: Arithmetic Operations, Prev: Comparison of Numbers, Up: Numbers
895 To convert an integer to floating point, use the function `float'.
897 - Function: float number
898 This returns NUMBER converted to floating point. If NUMBER is
899 already a floating point number, `float' returns it unchanged.
901 There are four functions to convert floating point numbers to
902 integers; they differ in how they round. These functions accept
903 integer arguments also, and return such arguments unchanged.
905 - Function: truncate number
906 This returns NUMBER, converted to an integer by rounding towards
909 - Function: floor number &optional divisor
910 This returns NUMBER, converted to an integer by rounding downward
911 (towards negative infinity).
913 If DIVISOR is specified, NUMBER is divided by DIVISOR before the
914 floor is taken; this is the division operation that corresponds to
915 `mod'. An `arith-error' results if DIVISOR is 0.
917 - Function: ceiling number
918 This returns NUMBER, converted to an integer by rounding upward
919 (towards positive infinity).
921 - Function: round number
922 This returns NUMBER, converted to an integer by rounding towards
923 the nearest integer. Rounding a value equidistant between two
924 integers may choose the integer closer to zero, or it may prefer
925 an even integer, depending on your machine.
928 File: lispref.info, Node: Arithmetic Operations, Next: Rounding Operations, Prev: Numeric Conversions, Up: Numbers
930 Arithmetic Operations
931 =====================
933 XEmacs Lisp provides the traditional four arithmetic operations:
934 addition, subtraction, multiplication, and division. Remainder and
935 modulus functions supplement the division functions. The functions to
936 add or subtract 1 are provided because they are traditional in Lisp and
939 All of these functions except `%' return a floating point value if
940 any argument is floating.
942 It is important to note that in XEmacs Lisp, arithmetic functions do
943 not check for overflow. Thus `(1+ 134217727)' may evaluate to
944 -134217728, depending on your hardware.
946 - Function: 1+ number-or-marker
947 This function returns NUMBER-OR-MARKER plus 1. For example,
954 This function is not analogous to the C operator `++'--it does not
955 increment a variable. It just computes a sum. Thus, if we
961 If you want to increment the variable, you must use `setq', like
967 Now that the `cl' package is always available from lisp code, a
968 more convenient and natural way to increment a variable is
971 - Function: 1- number-or-marker
972 This function returns NUMBER-OR-MARKER minus 1.
974 - Function: abs number
975 This returns the absolute value of NUMBER.
977 - Function: + &rest numbers-or-markers
978 This function adds its arguments together. When given no
979 arguments, `+' returns 0.
988 - Function: - &optional number-or-marker &rest other-numbers-or-markers
989 The `-' function serves two purposes: negation and subtraction.
990 When `-' has a single argument, the value is the negative of the
991 argument. When there are multiple arguments, `-' subtracts each of
992 the OTHER-NUMBERS-OR-MARKERS from NUMBER-OR-MARKER, cumulatively.
993 If there are no arguments, the result is 0.
1002 - Function: * &rest numbers-or-markers
1003 This function multiplies its arguments together, and returns the
1004 product. When given no arguments, `*' returns 1.
1013 - Function: / dividend divisor &rest divisors
1014 This function divides DIVIDEND by DIVISOR and returns the
1015 quotient. If there are additional arguments DIVISORS, then it
1016 divides DIVIDEND by each divisor in turn. Each argument may be a
1019 If all the arguments are integers, then the result is an integer
1020 too. This means the result has to be rounded. On most machines,
1021 the result is rounded towards zero after each division, but some
1022 machines may round differently with negative arguments. This is
1023 because the Lisp function `/' is implemented using the C division
1024 operator, which also permits machine-dependent rounding. As a
1025 practical matter, all known machines round in the standard fashion.
1027 If you divide by 0, an `arith-error' error is signaled. (*Note
1039 The result of `(/ -17 6)' could in principle be -3 on some
1042 - Function: % dividend divisor
1043 This function returns the integer remainder after division of
1044 DIVIDEND by DIVISOR. The arguments must be integers or markers.
1046 For negative arguments, the remainder is in principle
1047 machine-dependent since the quotient is; but in practice, all
1048 known machines behave alike.
1050 An `arith-error' results if DIVISOR is 0.
1061 For any two integers DIVIDEND and DIVISOR,
1063 (+ (% DIVIDEND DIVISOR)
1064 (* (/ DIVIDEND DIVISOR) DIVISOR))
1066 always equals DIVIDEND.
1068 - Function: mod dividend divisor
1069 This function returns the value of DIVIDEND modulo DIVISOR; in
1070 other words, the remainder after division of DIVIDEND by DIVISOR,
1071 but with the same sign as DIVISOR. The arguments must be numbers
1074 Unlike `%', `mod' returns a well-defined result for negative
1075 arguments. It also permits floating point arguments; it rounds the
1076 quotient downward (towards minus infinity) to an integer, and uses
1077 that quotient to compute the remainder.
1079 An `arith-error' results if DIVISOR is 0.
1092 For any two numbers DIVIDEND and DIVISOR,
1094 (+ (mod DIVIDEND DIVISOR)
1095 (* (floor DIVIDEND DIVISOR) DIVISOR))
1097 always equals DIVIDEND, subject to rounding error if either
1098 argument is floating point. For `floor', see *Note Numeric
1102 File: lispref.info, Node: Rounding Operations, Next: Bitwise Operations, Prev: Arithmetic Operations, Up: Numbers
1107 The functions `ffloor', `fceiling', `fround' and `ftruncate' take a
1108 floating point argument and return a floating point result whose value
1109 is a nearby integer. `ffloor' returns the nearest integer below;
1110 `fceiling', the nearest integer above; `ftruncate', the nearest integer
1111 in the direction towards zero; `fround', the nearest integer.
1113 - Function: ffloor float
1114 This function rounds FLOAT to the next lower integral value, and
1115 returns that value as a floating point number.
1117 - Function: fceiling float
1118 This function rounds FLOAT to the next higher integral value, and
1119 returns that value as a floating point number.
1121 - Function: ftruncate float
1122 This function rounds FLOAT towards zero to an integral value, and
1123 returns that value as a floating point number.
1125 - Function: fround float
1126 This function rounds FLOAT to the nearest integral value, and
1127 returns that value as a floating point number.
1130 File: lispref.info, Node: Bitwise Operations, Next: Math Functions, Prev: Rounding Operations, Up: Numbers
1132 Bitwise Operations on Integers
1133 ==============================
1135 In a computer, an integer is represented as a binary number, a
1136 sequence of "bits" (digits which are either zero or one). A bitwise
1137 operation acts on the individual bits of such a sequence. For example,
1138 "shifting" moves the whole sequence left or right one or more places,
1139 reproducing the same pattern "moved over".
1141 The bitwise operations in XEmacs Lisp apply only to integers.
1143 - Function: lsh integer1 count
1144 `lsh', which is an abbreviation for "logical shift", shifts the
1145 bits in INTEGER1 to the left COUNT places, or to the right if
1146 COUNT is negative, bringing zeros into the vacated bits. If COUNT
1147 is negative, `lsh' shifts zeros into the leftmost
1148 (most-significant) bit, producing a positive result even if
1149 INTEGER1 is negative. Contrast this with `ash', below.
1151 Here are two examples of `lsh', shifting a pattern of bits one
1152 place to the left. We show only the low-order eight bits of the
1153 binary pattern; the rest are all zero.
1157 ;; Decimal 5 becomes decimal 10.
1158 00000101 => 00001010
1162 ;; Decimal 7 becomes decimal 14.
1163 00000111 => 00001110
1165 As the examples illustrate, shifting the pattern of bits one place
1166 to the left produces a number that is twice the value of the
1169 Shifting a pattern of bits two places to the left produces results
1170 like this (with 8-bit binary numbers):
1174 ;; Decimal 3 becomes decimal 12.
1175 00000011 => 00001100
1177 On the other hand, shifting one place to the right looks like this:
1181 ;; Decimal 6 becomes decimal 3.
1182 00000110 => 00000011
1186 ;; Decimal 5 becomes decimal 2.
1187 00000101 => 00000010
1189 As the example illustrates, shifting one place to the right
1190 divides the value of a positive integer by two, rounding downward.
1192 The function `lsh', like all XEmacs Lisp arithmetic functions, does
1193 not check for overflow, so shifting left can discard significant
1194 bits and change the sign of the number. For example, left shifting
1195 134,217,727 produces -2 on a 28-bit machine:
1197 (lsh 134217727 1) ; left shift
1200 In binary, in the 28-bit implementation, the argument looks like
1203 ;; Decimal 134,217,727
1204 0111 1111 1111 1111 1111 1111 1111
1206 which becomes the following when left shifted:
1209 1111 1111 1111 1111 1111 1111 1110
1211 - Function: ash integer1 count
1212 `ash' ("arithmetic shift") shifts the bits in INTEGER1 to the left
1213 COUNT places, or to the right if COUNT is negative.
1215 `ash' gives the same results as `lsh' except when INTEGER1 and
1216 COUNT are both negative. In that case, `ash' puts ones in the
1217 empty bit positions on the left, while `lsh' puts zeros in those
1220 Thus, with `ash', shifting the pattern of bits one place to the
1221 right looks like this:
1224 ;; Decimal -6 becomes decimal -3.
1225 1111 1111 1111 1111 1111 1111 1010
1227 1111 1111 1111 1111 1111 1111 1101
1229 In contrast, shifting the pattern of bits one place to the right
1230 with `lsh' looks like this:
1232 (lsh -6 -1) => 134217725
1233 ;; Decimal -6 becomes decimal 134,217,725.
1234 1111 1111 1111 1111 1111 1111 1010
1236 0111 1111 1111 1111 1111 1111 1101
1238 Here are other examples:
1240 ; 28-bit binary values
1242 (lsh 5 2) ; 5 = 0000 0000 0000 0000 0000 0000 0101
1243 => 20 ; = 0000 0000 0000 0000 0000 0001 0100
1246 (lsh -5 2) ; -5 = 1111 1111 1111 1111 1111 1111 1011
1247 => -20 ; = 1111 1111 1111 1111 1111 1110 1100
1250 (lsh 5 -2) ; 5 = 0000 0000 0000 0000 0000 0000 0101
1251 => 1 ; = 0000 0000 0000 0000 0000 0000 0001
1254 (lsh -5 -2) ; -5 = 1111 1111 1111 1111 1111 1111 1011
1255 => 4194302 ; = 0011 1111 1111 1111 1111 1111 1110
1256 (ash -5 -2) ; -5 = 1111 1111 1111 1111 1111 1111 1011
1257 => -2 ; = 1111 1111 1111 1111 1111 1111 1110
1259 - Function: logand &rest ints-or-markers
1260 This function returns the "logical and" of the arguments: the Nth
1261 bit is set in the result if, and only if, the Nth bit is set in
1262 all the arguments. ("Set" means that the value of the bit is 1
1265 For example, using 4-bit binary numbers, the "logical and" of 13
1266 and 12 is 12: 1101 combined with 1100 produces 1100. In both the
1267 binary numbers, the leftmost two bits are set (i.e., they are
1268 1's), so the leftmost two bits of the returned value are set.
1269 However, for the rightmost two bits, each is zero in at least one
1270 of the arguments, so the rightmost two bits of the returned value
1278 If `logand' is not passed any argument, it returns a value of -1.
1279 This number is an identity element for `logand' because its binary
1280 representation consists entirely of ones. If `logand' is passed
1281 just one argument, it returns that argument.
1283 ; 28-bit binary values
1285 (logand 14 13) ; 14 = 0000 0000 0000 0000 0000 0000 1110
1286 ; 13 = 0000 0000 0000 0000 0000 0000 1101
1287 => 12 ; 12 = 0000 0000 0000 0000 0000 0000 1100
1289 (logand 14 13 4) ; 14 = 0000 0000 0000 0000 0000 0000 1110
1290 ; 13 = 0000 0000 0000 0000 0000 0000 1101
1291 ; 4 = 0000 0000 0000 0000 0000 0000 0100
1292 => 4 ; 4 = 0000 0000 0000 0000 0000 0000 0100
1295 => -1 ; -1 = 1111 1111 1111 1111 1111 1111 1111
1297 - Function: logior &rest ints-or-markers
1298 This function returns the "inclusive or" of its arguments: the Nth
1299 bit is set in the result if, and only if, the Nth bit is set in at
1300 least one of the arguments. If there are no arguments, the result
1301 is zero, which is an identity element for this operation. If
1302 `logior' is passed just one argument, it returns that argument.
1304 ; 28-bit binary values
1306 (logior 12 5) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1307 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1308 => 13 ; 13 = 0000 0000 0000 0000 0000 0000 1101
1310 (logior 12 5 7) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1311 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1312 ; 7 = 0000 0000 0000 0000 0000 0000 0111
1313 => 15 ; 15 = 0000 0000 0000 0000 0000 0000 1111
1315 - Function: logxor &rest ints-or-markers
1316 This function returns the "exclusive or" of its arguments: the Nth
1317 bit is set in the result if, and only if, the Nth bit is set in an
1318 odd number of the arguments. If there are no arguments, the
1319 result is 0, which is an identity element for this operation. If
1320 `logxor' is passed just one argument, it returns that argument.
1322 ; 28-bit binary values
1324 (logxor 12 5) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1325 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1326 => 9 ; 9 = 0000 0000 0000 0000 0000 0000 1001
1328 (logxor 12 5 7) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1329 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1330 ; 7 = 0000 0000 0000 0000 0000 0000 0111
1331 => 14 ; 14 = 0000 0000 0000 0000 0000 0000 1110
1333 - Function: lognot integer
1334 This function returns the logical complement of its argument: the
1335 Nth bit is one in the result if, and only if, the Nth bit is zero
1336 in INTEGER, and vice-versa.
1340 ;; 5 = 0000 0000 0000 0000 0000 0000 0101
1342 ;; -6 = 1111 1111 1111 1111 1111 1111 1010
1345 File: lispref.info, Node: Math Functions, Next: Random Numbers, Prev: Bitwise Operations, Up: Numbers
1347 Standard Mathematical Functions
1348 ===============================
1350 These mathematical functions are available if floating point is
1351 supported (which is the normal state of affairs). They allow integers
1352 as well as floating point numbers as arguments.
1357 These are the ordinary trigonometric functions, with argument
1358 measured in radians.
1360 - Function: asin arg
1361 The value of `(asin ARG)' is a number between -pi/2 and pi/2
1362 (inclusive) whose sine is ARG; if, however, ARG is out of range
1363 (outside [-1, 1]), then the result is a NaN.
1365 - Function: acos arg
1366 The value of `(acos ARG)' is a number between 0 and pi (inclusive)
1367 whose cosine is ARG; if, however, ARG is out of range (outside
1368 [-1, 1]), then the result is a NaN.
1370 - Function: atan arg
1371 The value of `(atan ARG)' is a number between -pi/2 and pi/2
1372 (exclusive) whose tangent is ARG.
1374 - Function: sinh arg
1375 - Function: cosh arg
1376 - Function: tanh arg
1377 These are the ordinary hyperbolic trigonometric functions.
1379 - Function: asinh arg
1380 - Function: acosh arg
1381 - Function: atanh arg
1382 These are the inverse hyperbolic trigonometric functions.
1385 This is the exponential function; it returns e to the power ARG.
1386 e is a fundamental mathematical constant also called the base of
1389 - Function: log arg &optional base
1390 This function returns the logarithm of ARG, with base BASE. If
1391 you don't specify BASE, the base E is used. If ARG is negative,
1392 the result is a NaN.
1394 - Function: log10 arg
1395 This function returns the logarithm of ARG, with base 10. If ARG
1396 is negative, the result is a NaN. `(log10 X)' == `(log X 10)', at
1397 least approximately.
1399 - Function: expt x y
1400 This function returns X raised to power Y. If both arguments are
1401 integers and Y is positive, the result is an integer; in this
1402 case, it is truncated to fit the range of possible integer values.
1404 - Function: sqrt arg
1405 This returns the square root of ARG. If ARG is negative, the
1408 - Function: cube-root arg
1409 This returns the cube root of ARG.
1412 File: lispref.info, Node: Random Numbers, Prev: Math Functions, Up: Numbers
1417 A deterministic computer program cannot generate true random numbers.
1418 For most purposes, "pseudo-random numbers" suffice. A series of
1419 pseudo-random numbers is generated in a deterministic fashion. The
1420 numbers are not truly random, but they have certain properties that
1421 mimic a random series. For example, all possible values occur equally
1422 often in a pseudo-random series.
1424 In XEmacs, pseudo-random numbers are generated from a "seed" number.
1425 Starting from any given seed, the `random' function always generates
1426 the same sequence of numbers. XEmacs always starts with the same seed
1427 value, so the sequence of values of `random' is actually the same in
1428 each XEmacs run! For example, in one operating system, the first call
1429 to `(random)' after you start XEmacs always returns -1457731, and the
1430 second one always returns -7692030. This repeatability is helpful for
1433 If you want truly unpredictable random numbers, execute `(random
1434 t)'. This chooses a new seed based on the current time of day and on
1435 XEmacs's process ID number.
1437 - Function: random &optional limit
1438 This function returns a pseudo-random integer. Repeated calls
1439 return a series of pseudo-random integers.
1441 If LIMIT is a positive integer, the value is chosen to be
1442 nonnegative and less than LIMIT.
1444 If LIMIT is `t', it means to choose a new seed based on the
1445 current time of day and on XEmacs's process ID number.
1447 On some machines, any integer representable in Lisp may be the
1448 result of `random'. On other machines, the result can never be
1449 larger than a certain maximum or less than a certain (negative)
1453 File: lispref.info, Node: Strings and Characters, Next: Lists, Prev: Numbers, Up: Top
1455 Strings and Characters
1456 **********************
1458 A string in XEmacs Lisp is an array that contains an ordered sequence
1459 of characters. Strings are used as names of symbols, buffers, and
1460 files, to send messages to users, to hold text being copied between
1461 buffers, and for many other purposes. Because strings are so important,
1462 XEmacs Lisp has many functions expressly for manipulating them. XEmacs
1463 Lisp programs use strings more often than individual characters.
1467 * Basics: String Basics. Basic properties of strings and characters.
1468 * Predicates for Strings:: Testing whether an object is a string or char.
1469 * Creating Strings:: Functions to allocate new strings.
1470 * Predicates for Characters:: Testing whether an object is a character.
1471 * Character Codes:: Each character has an equivalent integer.
1472 * Text Comparison:: Comparing characters or strings.
1473 * String Conversion:: Converting characters or strings and vice versa.
1474 * Modifying Strings:: Changing characters in a string.
1475 * String Properties:: Additional information attached to strings.
1476 * Formatting Strings:: `format': XEmacs's analog of `printf'.
1477 * Character Case:: Case conversion functions.
1478 * Case Tables:: Customizing case conversion.
1479 * Char Tables:: Mapping from characters to Lisp objects.