1 This is ../info/lispref.info, produced by makeinfo version 4.0b 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 object1 object2
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
947 This function returns NUMBER plus one. NUMBER may be a number,
948 character or marker. Markers and characters are converted to
958 This function is not analogous to the C operator `++'--it does not
959 increment a variable. It just computes a sum. Thus, if we
965 If you want to increment the variable, you must use `setq', like
971 Now that the `cl' package is always available from lisp code, a
972 more convenient and natural way to increment a variable is
975 - Function: 1- number
976 This function returns NUMBER minus one. NUMBER may be a number,
977 character or marker. Markers and characters are converted to
980 - Function: abs number
981 This returns the absolute value of NUMBER.
983 - Function: + &rest numbers
984 This function adds its arguments together. When given no
985 arguments, `+' returns 0.
987 If any of the arguments are characters or markers, they are first
988 converted to integers.
997 - Function: - &optional number &rest other-numbers
998 The `-' function serves two purposes: negation and subtraction.
999 When `-' has a single argument, the value is the negative of the
1000 argument. When there are multiple arguments, `-' subtracts each of
1001 the OTHER-NUMBERS from NUMBER, cumulatively. If there are no
1002 arguments, an error is signaled.
1004 If any of the arguments are characters or markers, they are first
1005 converted to integers.
1014 - Function: * &rest numbers
1015 This function multiplies its arguments together, and returns the
1016 product. When given no arguments, `*' returns 1.
1018 If any of the arguments are characters or markers, they are first
1019 converted to integers.
1028 - Function: / dividend &rest divisors
1029 The `/' function serves two purposes: inversion and division. When
1030 `/' has a single argument, the value is the inverse of the
1031 argument. When there are multiple arguments, `/' divides DIVIDEND
1032 by each of the DIVISORS, cumulatively, returning the quotient. If
1033 there are no arguments, an error is signaled.
1035 If none of the arguments are floats, then the result is an integer.
1036 This means the result has to be rounded. On most machines, the
1037 result is rounded towards zero after each division, but some
1038 machines may round differently with negative arguments. This is
1039 because the Lisp function `/' is implemented using the C division
1040 operator, which also permits machine-dependent rounding. As a
1041 practical matter, all known machines round in the standard fashion.
1043 If any of the arguments are characters or markers, they are first
1044 converted to integers.
1046 If you divide by 0, an `arith-error' error is signaled. (*Note
1056 => 0.3333333333333333
1060 The result of `(/ -17 6)' could in principle be -3 on some
1063 - Function: % dividend divisor
1064 This function returns the integer remainder after division of
1065 DIVIDEND by DIVISOR. The arguments must be integers or markers.
1067 For negative arguments, the remainder is in principle
1068 machine-dependent since the quotient is; but in practice, all
1069 known machines behave alike.
1071 An `arith-error' results if DIVISOR is 0.
1082 For any two integers DIVIDEND and DIVISOR,
1084 (+ (% DIVIDEND DIVISOR)
1085 (* (/ DIVIDEND DIVISOR) DIVISOR))
1087 always equals DIVIDEND.
1089 - Function: mod dividend divisor
1090 This function returns the value of DIVIDEND modulo DIVISOR; in
1091 other words, the remainder after division of DIVIDEND by DIVISOR,
1092 but with the same sign as DIVISOR. The arguments must be numbers
1095 Unlike `%', `mod' returns a well-defined result for negative
1096 arguments. It also permits floating point arguments; it rounds the
1097 quotient downward (towards minus infinity) to an integer, and uses
1098 that quotient to compute the remainder.
1100 An `arith-error' results if DIVISOR is 0.
1113 For any two numbers DIVIDEND and DIVISOR,
1115 (+ (mod DIVIDEND DIVISOR)
1116 (* (floor DIVIDEND DIVISOR) DIVISOR))
1118 always equals DIVIDEND, subject to rounding error if either
1119 argument is floating point. For `floor', see *Note Numeric
1123 File: lispref.info, Node: Rounding Operations, Next: Bitwise Operations, Prev: Arithmetic Operations, Up: Numbers
1128 The functions `ffloor', `fceiling', `fround' and `ftruncate' take a
1129 floating point argument and return a floating point result whose value
1130 is a nearby integer. `ffloor' returns the nearest integer below;
1131 `fceiling', the nearest integer above; `ftruncate', the nearest integer
1132 in the direction towards zero; `fround', the nearest integer.
1134 - Function: ffloor number
1135 This function rounds NUMBER to the next lower integral value, and
1136 returns that value as a floating point number.
1138 - Function: fceiling number
1139 This function rounds NUMBER to the next higher integral value, and
1140 returns that value as a floating point number.
1142 - Function: ftruncate number
1143 This function rounds NUMBER towards zero to an integral value, and
1144 returns that value as a floating point number.
1146 - Function: fround number
1147 This function rounds NUMBER to the nearest integral value, and
1148 returns that value as a floating point number.
1151 File: lispref.info, Node: Bitwise Operations, Next: Math Functions, Prev: Rounding Operations, Up: Numbers
1153 Bitwise Operations on Integers
1154 ==============================
1156 In a computer, an integer is represented as a binary number, a
1157 sequence of "bits" (digits which are either zero or one). A bitwise
1158 operation acts on the individual bits of such a sequence. For example,
1159 "shifting" moves the whole sequence left or right one or more places,
1160 reproducing the same pattern "moved over".
1162 The bitwise operations in XEmacs Lisp apply only to integers.
1164 - Function: lsh integer1 count
1165 `lsh', which is an abbreviation for "logical shift", shifts the
1166 bits in INTEGER1 to the left COUNT places, or to the right if
1167 COUNT is negative, bringing zeros into the vacated bits. If COUNT
1168 is negative, `lsh' shifts zeros into the leftmost
1169 (most-significant) bit, producing a positive result even if
1170 INTEGER1 is negative. Contrast this with `ash', below.
1172 Here are two examples of `lsh', shifting a pattern of bits one
1173 place to the left. We show only the low-order eight bits of the
1174 binary pattern; the rest are all zero.
1178 ;; Decimal 5 becomes decimal 10.
1179 00000101 => 00001010
1183 ;; Decimal 7 becomes decimal 14.
1184 00000111 => 00001110
1186 As the examples illustrate, shifting the pattern of bits one place
1187 to the left produces a number that is twice the value of the
1190 Shifting a pattern of bits two places to the left produces results
1191 like this (with 8-bit binary numbers):
1195 ;; Decimal 3 becomes decimal 12.
1196 00000011 => 00001100
1198 On the other hand, shifting one place to the right looks like this:
1202 ;; Decimal 6 becomes decimal 3.
1203 00000110 => 00000011
1207 ;; Decimal 5 becomes decimal 2.
1208 00000101 => 00000010
1210 As the example illustrates, shifting one place to the right
1211 divides the value of a positive integer by two, rounding downward.
1213 The function `lsh', like all XEmacs Lisp arithmetic functions, does
1214 not check for overflow, so shifting left can discard significant
1215 bits and change the sign of the number. For example, left shifting
1216 134,217,727 produces -2 on a 28-bit machine:
1218 (lsh 134217727 1) ; left shift
1221 In binary, in the 28-bit implementation, the argument looks like
1224 ;; Decimal 134,217,727
1225 0111 1111 1111 1111 1111 1111 1111
1227 which becomes the following when left shifted:
1230 1111 1111 1111 1111 1111 1111 1110
1232 - Function: ash integer1 count
1233 `ash' ("arithmetic shift") shifts the bits in INTEGER1 to the left
1234 COUNT places, or to the right if COUNT is negative.
1236 `ash' gives the same results as `lsh' except when INTEGER1 and
1237 COUNT are both negative. In that case, `ash' puts ones in the
1238 empty bit positions on the left, while `lsh' puts zeros in those
1241 Thus, with `ash', shifting the pattern of bits one place to the
1242 right looks like this:
1245 ;; Decimal -6 becomes decimal -3.
1246 1111 1111 1111 1111 1111 1111 1010
1248 1111 1111 1111 1111 1111 1111 1101
1250 In contrast, shifting the pattern of bits one place to the right
1251 with `lsh' looks like this:
1253 (lsh -6 -1) => 134217725
1254 ;; Decimal -6 becomes decimal 134,217,725.
1255 1111 1111 1111 1111 1111 1111 1010
1257 0111 1111 1111 1111 1111 1111 1101
1259 Here are other examples:
1261 ; 28-bit binary values
1263 (lsh 5 2) ; 5 = 0000 0000 0000 0000 0000 0000 0101
1264 => 20 ; = 0000 0000 0000 0000 0000 0001 0100
1267 (lsh -5 2) ; -5 = 1111 1111 1111 1111 1111 1111 1011
1268 => -20 ; = 1111 1111 1111 1111 1111 1110 1100
1271 (lsh 5 -2) ; 5 = 0000 0000 0000 0000 0000 0000 0101
1272 => 1 ; = 0000 0000 0000 0000 0000 0000 0001
1275 (lsh -5 -2) ; -5 = 1111 1111 1111 1111 1111 1111 1011
1276 => 4194302 ; = 0011 1111 1111 1111 1111 1111 1110
1277 (ash -5 -2) ; -5 = 1111 1111 1111 1111 1111 1111 1011
1278 => -2 ; = 1111 1111 1111 1111 1111 1111 1110
1280 - Function: logand &rest ints-or-markers
1281 This function returns the "logical and" of the arguments: the Nth
1282 bit is set in the result if, and only if, the Nth bit is set in
1283 all the arguments. ("Set" means that the value of the bit is 1
1286 For example, using 4-bit binary numbers, the "logical and" of 13
1287 and 12 is 12: 1101 combined with 1100 produces 1100. In both the
1288 binary numbers, the leftmost two bits are set (i.e., they are
1289 1's), so the leftmost two bits of the returned value are set.
1290 However, for the rightmost two bits, each is zero in at least one
1291 of the arguments, so the rightmost two bits of the returned value
1299 If `logand' is not passed any argument, it returns a value of -1.
1300 This number is an identity element for `logand' because its binary
1301 representation consists entirely of ones. If `logand' is passed
1302 just one argument, it returns that argument.
1304 ; 28-bit binary values
1306 (logand 14 13) ; 14 = 0000 0000 0000 0000 0000 0000 1110
1307 ; 13 = 0000 0000 0000 0000 0000 0000 1101
1308 => 12 ; 12 = 0000 0000 0000 0000 0000 0000 1100
1310 (logand 14 13 4) ; 14 = 0000 0000 0000 0000 0000 0000 1110
1311 ; 13 = 0000 0000 0000 0000 0000 0000 1101
1312 ; 4 = 0000 0000 0000 0000 0000 0000 0100
1313 => 4 ; 4 = 0000 0000 0000 0000 0000 0000 0100
1316 => -1 ; -1 = 1111 1111 1111 1111 1111 1111 1111
1318 - Function: logior &rest ints-or-markers
1319 This function returns the "inclusive or" of its arguments: the Nth
1320 bit is set in the result if, and only if, the Nth bit is set in at
1321 least one of the arguments. If there are no arguments, the result
1322 is zero, which is an identity element for this operation. If
1323 `logior' is passed just one argument, it returns that argument.
1325 ; 28-bit binary values
1327 (logior 12 5) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1328 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1329 => 13 ; 13 = 0000 0000 0000 0000 0000 0000 1101
1331 (logior 12 5 7) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1332 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1333 ; 7 = 0000 0000 0000 0000 0000 0000 0111
1334 => 15 ; 15 = 0000 0000 0000 0000 0000 0000 1111
1336 - Function: logxor &rest ints-or-markers
1337 This function returns the "exclusive or" of its arguments: the Nth
1338 bit is set in the result if, and only if, the Nth bit is set in an
1339 odd number of the arguments. If there are no arguments, the
1340 result is 0, which is an identity element for this operation. If
1341 `logxor' is passed just one argument, it returns that argument.
1343 ; 28-bit binary values
1345 (logxor 12 5) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1346 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1347 => 9 ; 9 = 0000 0000 0000 0000 0000 0000 1001
1349 (logxor 12 5 7) ; 12 = 0000 0000 0000 0000 0000 0000 1100
1350 ; 5 = 0000 0000 0000 0000 0000 0000 0101
1351 ; 7 = 0000 0000 0000 0000 0000 0000 0111
1352 => 14 ; 14 = 0000 0000 0000 0000 0000 0000 1110
1354 - Function: lognot integer
1355 This function returns the logical complement of its argument: the
1356 Nth bit is one in the result if, and only if, the Nth bit is zero
1357 in INTEGER, and vice-versa.
1361 ;; 5 = 0000 0000 0000 0000 0000 0000 0101
1363 ;; -6 = 1111 1111 1111 1111 1111 1111 1010
1366 File: lispref.info, Node: Math Functions, Next: Random Numbers, Prev: Bitwise Operations, Up: Numbers
1368 Standard Mathematical Functions
1369 ===============================
1371 These mathematical functions are available if floating point is
1372 supported (which is the normal state of affairs). They allow integers
1373 as well as floating point numbers as arguments.
1375 - Function: sin number
1376 - Function: cos number
1377 - Function: tan number
1378 These are the ordinary trigonometric functions, with argument
1379 measured in radians.
1381 - Function: asin number
1382 The value of `(asin NUMBER)' is a number between -pi/2 and pi/2
1383 (inclusive) whose sine is NUMBER; if, however, NUMBER is out of
1384 range (outside [-1, 1]), then the result is a NaN.
1386 - Function: acos number
1387 The value of `(acos NUMBER)' is a number between 0 and pi
1388 (inclusive) whose cosine is NUMBER; if, however, NUMBER is out of
1389 range (outside [-1, 1]), then the result is a NaN.
1391 - Function: atan number &optional number2
1392 The value of `(atan NUMBER)' is a number between -pi/2 and pi/2
1393 (exclusive) whose tangent is NUMBER.
1395 If optional argument NUMBER2 is supplied, the function returns
1396 `atan2(NUMBER,NUMBER2)'.
1398 - Function: sinh number
1399 - Function: cosh number
1400 - Function: tanh number
1401 These are the ordinary hyperbolic trigonometric functions.
1403 - Function: asinh number
1404 - Function: acosh number
1405 - Function: atanh number
1406 These are the inverse hyperbolic trigonometric functions.
1408 - Function: exp number
1409 This is the exponential function; it returns e to the power
1410 NUMBER. e is a fundamental mathematical constant also called the
1411 base of natural logarithms.
1413 - Function: log number &optional base
1414 This function returns the logarithm of NUMBER, with base BASE. If
1415 you don't specify BASE, the base E is used. If NUMBER is
1416 negative, the result is a NaN.
1418 - Function: log10 number
1419 This function returns the logarithm of NUMBER, with base 10. If
1420 NUMBER is negative, the result is a NaN. `(log10 X)' == `(log X
1421 10)', at least approximately.
1423 - Function: expt x y
1424 This function returns X raised to power Y. If both arguments are
1425 integers and Y is positive, the result is an integer; in this
1426 case, it is truncated to fit the range of possible integer values.
1428 - Function: sqrt number
1429 This returns the square root of NUMBER. If NUMBER is negative,
1432 - Function: cube-root number
1433 This returns the cube root of NUMBER.
1436 File: lispref.info, Node: Random Numbers, Prev: Math Functions, Up: Numbers
1441 A deterministic computer program cannot generate true random numbers.
1442 For most purposes, "pseudo-random numbers" suffice. A series of
1443 pseudo-random numbers is generated in a deterministic fashion. The
1444 numbers are not truly random, but they have certain properties that
1445 mimic a random series. For example, all possible values occur equally
1446 often in a pseudo-random series.
1448 In XEmacs, pseudo-random numbers are generated from a "seed" number.
1449 Starting from any given seed, the `random' function always generates
1450 the same sequence of numbers. XEmacs always starts with the same seed
1451 value, so the sequence of values of `random' is actually the same in
1452 each XEmacs run! For example, in one operating system, the first call
1453 to `(random)' after you start XEmacs always returns -1457731, and the
1454 second one always returns -7692030. This repeatability is helpful for
1457 If you want truly unpredictable random numbers, execute `(random
1458 t)'. This chooses a new seed based on the current time of day and on
1459 XEmacs's process ID number.
1461 - Function: random &optional limit
1462 This function returns a pseudo-random integer. Repeated calls
1463 return a series of pseudo-random integers.
1465 If LIMIT is a positive integer, the value is chosen to be
1466 nonnegative and less than LIMIT.
1468 If LIMIT is `t', it means to choose a new seed based on the
1469 current time of day and on XEmacs's process ID number.
1471 On some machines, any integer representable in Lisp may be the
1472 result of `random'. On other machines, the result can never be
1473 larger than a certain maximum or less than a certain (negative)
1477 File: lispref.info, Node: Strings and Characters, Next: Lists, Prev: Numbers, Up: Top
1479 Strings and Characters
1480 **********************
1482 A string in XEmacs Lisp is an array that contains an ordered sequence
1483 of characters. Strings are used as names of symbols, buffers, and
1484 files, to send messages to users, to hold text being copied between
1485 buffers, and for many other purposes. Because strings are so important,
1486 XEmacs Lisp has many functions expressly for manipulating them. XEmacs
1487 Lisp programs use strings more often than individual characters.
1491 * String Basics:: Basic properties of strings and characters.
1492 * Predicates for Strings:: Testing whether an object is a string or char.
1493 * Creating Strings:: Functions to allocate new strings.
1494 * Predicates for Characters:: Testing whether an object is a character.
1495 * Character Codes:: Each character has an equivalent integer.
1496 * Text Comparison:: Comparing characters or strings.
1497 * String Conversion:: Converting characters or strings and vice versa.
1498 * Modifying Strings:: Changing characters in a string.
1499 * String Properties:: Additional information attached to strings.
1500 * Formatting Strings:: `format': XEmacs's analog of `printf'.
1501 * Character Case:: Case conversion functions.
1502 * Case Tables:: Customizing case conversion.
1503 * Char Tables:: Mapping from characters to Lisp objects.