comparison lispref/functions.texi @ 21682:90da2489c498

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Mon, 20 Apr 1998 17:43:57 +0000
parents 66d807bdc5b4
children d4ac295a98b3
comparison
equal deleted inserted replaced
21681:11eafe90b842 21682:90da2489c498
110 byte compiler. @xref{Byte-Code Type}. 110 byte compiler. @xref{Byte-Code Type}.
111 @end table 111 @end table
112 112
113 @tindex functionp 113 @tindex functionp
114 @defun functionp object 114 @defun functionp object
115 This function returns @code{t} if @var{object} is any kind of function; 115 This function returns @code{t} if @var{object} is any kind of function,
116 that is, anything that could be called as a function. 116 or a special form or macro.
117 @end defun 117 @end defun
118 118
119 @defun subrp object 119 @defun subrp object
120 This function returns @code{t} if @var{object} is a built-in function 120 This function returns @code{t} if @var{object} is a built-in function
121 (i.e., a Lisp primitive). 121 (i.e., a Lisp primitive).
384 actually appears inside the Lisp world and can be used by the Emacs help 384 actually appears inside the Lisp world and can be used by the Emacs help
385 facilities. @xref{Documentation}, for how the @var{documentation-string} is 385 facilities. @xref{Documentation}, for how the @var{documentation-string} is
386 accessed. 386 accessed.
387 387
388 It is a good idea to provide documentation strings for all the 388 It is a good idea to provide documentation strings for all the
389 functions in your program, even those that are only called from within 389 functions in your program, even those that are called only from within
390 your program. Documentation strings are like comments, except that they 390 your program. Documentation strings are like comments, except that they
391 are easier to access. 391 are easier to access.
392 392
393 The first line of the documentation string should stand on its own, 393 The first line of the documentation string should stand on its own,
394 because @code{apropos} displays just this first line. It should consist 394 because @code{apropos} displays just this first line. It should consist
417 @cindex function name 417 @cindex function name
418 418
419 In most computer languages, every function has a name; the idea of a 419 In most computer languages, every function has a name; the idea of a
420 function without a name is nonsensical. In Lisp, a function in the 420 function without a name is nonsensical. In Lisp, a function in the
421 strictest sense has no name. It is simply a list whose first element is 421 strictest sense has no name. It is simply a list whose first element is
422 @code{lambda}, or a primitive subr-object. 422 @code{lambda}, a byte-code function object, or a primitive subr-object.
423 423
424 However, a symbol can serve as the name of a function. This happens 424 However, a symbol can serve as the name of a function. This happens
425 when you put the function in the symbol's @dfn{function cell} 425 when you put the function in the symbol's @dfn{function cell}
426 (@pxref{Symbol Components}). Then the symbol itself becomes a valid, 426 (@pxref{Symbol Components}). Then the symbol itself becomes a valid,
427 callable function, equivalent to the list or subr-object that its 427 callable function, equivalent to the list or subr-object that its
578 When you write a list as an expression in your program, the function 578 When you write a list as an expression in your program, the function
579 name it calls is written in your program. This means that you choose 579 name it calls is written in your program. This means that you choose
580 which function to call, and how many arguments to give it, when you 580 which function to call, and how many arguments to give it, when you
581 write the program. Usually that's just what you want. Occasionally you 581 write the program. Usually that's just what you want. Occasionally you
582 need to compute at run time which function to call. To do that, use the 582 need to compute at run time which function to call. To do that, use the
583 functions @code{funcall} and @code{apply}. 583 function @code{funcall}. When you also need to determine at run time
584 how may arguments to pass, use @code{apply}.
584 585
585 @defun funcall function &rest arguments 586 @defun funcall function &rest arguments
586 @code{funcall} calls @var{function} with @var{arguments}, and returns 587 @code{funcall} calls @var{function} with @var{arguments}, and returns
587 whatever @var{function} returns. 588 whatever @var{function} returns.
588 589
688 689
689 A @dfn{mapping function} applies a given function to each element of a 690 A @dfn{mapping function} applies a given function to each element of a
690 list or other collection. Emacs Lisp has several such functions; 691 list or other collection. Emacs Lisp has several such functions;
691 @code{mapcar} and @code{mapconcat}, which scan a list, are described 692 @code{mapcar} and @code{mapconcat}, which scan a list, are described
692 here. @xref{Creating Symbols}, for the function @code{mapatoms} which 693 here. @xref{Creating Symbols}, for the function @code{mapatoms} which
693 maps over the symbols in an obarray. @xref{Char-Tables}, for the 694 maps over the symbols in an obarray.
694 function @code{map-char-table}, which maps over the elements in a 695
695 char-table. 696 These mapping functions do not allow char-tables because a char-table
697 is a sparse array whose nominal range of indices is very large. To map
698 over a char-table in a way that deals properly with its sparse nature,
699 use the function @code{map-char-table} (@pxref{Char-Tables}).
696 700
697 @defun mapcar function sequence 701 @defun mapcar function sequence
698 @code{mapcar} applies @var{function} to each element of @var{sequence} 702 @code{mapcar} applies @var{function} to each element of @var{sequence}
699 in turn, and returns a list of the results. 703 in turn, and returns a list of the results.
700 704
701 The argument @var{sequence} may be a list, a vector, or a string. The 705 The argument @var{sequence} can be any kind of sequence except a
706 char-table; that is, a list, a vector, a bool-vector, or a string. The
702 result is always a list. The length of the result is the same as the 707 result is always a list. The length of the result is the same as the
703 length of @var{sequence}. 708 length of @var{sequence}.
704 709
705 @smallexample 710 @smallexample
706 @group 711 @group
718 ;; @r{Call each function in @code{my-hooks}.} 723 ;; @r{Call each function in @code{my-hooks}.}
719 (mapcar 'funcall my-hooks) 724 (mapcar 'funcall my-hooks)
720 @end group 725 @end group
721 726
722 @group 727 @group
723 (defun mapcar* (f &rest args) 728 (defun mapcar* (function &rest args)
724 "Apply FUNCTION to successive cars of all ARGS. 729 "Apply FUNCTION to successive cars of all ARGS.
725 Return the list of results." 730 Return the list of results."
726 ;; @r{If no list is exhausted,} 731 ;; @r{If no list is exhausted,}
727 (if (not (memq 'nil args)) 732 (if (not (memq 'nil args))
728 ;; @r{apply function to @sc{CAR}s.} 733 ;; @r{apply function to @sc{car}s.}
729 (cons (apply f (mapcar 'car args)) 734 (cons (apply function (mapcar 'car args))
730 (apply 'mapcar* f 735 (apply 'mapcar* function
731 ;; @r{Recurse for rest of elements.} 736 ;; @r{Recurse for rest of elements.}
732 (mapcar 'cdr args))))) 737 (mapcar 'cdr args)))))
733 @end group 738 @end group
734 739
735 @group 740 @group
745 Between each pair of result strings, @code{mapconcat} inserts the string 750 Between each pair of result strings, @code{mapconcat} inserts the string
746 @var{separator}. Usually @var{separator} contains a space or comma or 751 @var{separator}. Usually @var{separator} contains a space or comma or
747 other suitable punctuation. 752 other suitable punctuation.
748 753
749 The argument @var{function} must be a function that can take one 754 The argument @var{function} must be a function that can take one
750 argument and return a string. 755 argument and return a string. The argument @var{sequence} can be any
756 kind of sequence except a char-table; that is, a list, a vector, a
757 bool-vector, or a string.
751 758
752 @smallexample 759 @smallexample
753 @group 760 @group
754 (mapconcat 'symbol-name 761 (mapconcat 'symbol-name
755 '(The cat in the hat) 762 '(The cat in the hat)
821 @end group 828 @end group
822 @end example 829 @end example
823 830
824 @noindent 831 @noindent
825 Here we define a function that uses @code{change-property}, 832 Here we define a function that uses @code{change-property},
826 passing a function that doubles its argument: 833 passing it a function to double a number:
827 834
828 @example 835 @example
829 @group 836 @group
830 (defun double-property (symbol prop) 837 (defun double-property (symbol prop)
831 (change-property symbol prop '(lambda (x) (* 2 x)))) 838 (change-property symbol prop '(lambda (x) (* 2 x))))
976 983
977 There are three normal uses of this function: 984 There are three normal uses of this function:
978 985
979 @itemize @bullet 986 @itemize @bullet
980 @item 987 @item
981 Copying one symbol's function definition to another. (In other words, 988 Copying one symbol's function definition to another---in other words,
982 making an alternate name for a function.) 989 making an alternate name for a function. (If you think of this as the
990 definition of the new name, you should use @code{defalias} instead of
991 @code{fset}; see @ref{Defining Functions}.)
983 992
984 @item 993 @item
985 Giving a symbol a function definition that is not a list and therefore 994 Giving a symbol a function definition that is not a list and therefore
986 cannot be made with @code{defun}. For example, you can use @code{fset} 995 cannot be made with @code{defun}. For example, you can use @code{fset}
987 to give a symbol @code{s1} a function definition which is another symbol 996 to give a symbol @code{s1} a function definition which is another symbol
988 @code{s2}; then @code{s1} serves as an alias for whatever definition 997 @code{s2}; then @code{s1} serves as an alias for whatever definition
989 @code{s2} presently has. 998 @code{s2} presently has. (Once again use @code{defalias} instead of
999 @code{fset} if you think of this as the definition of @code{s1}.)
990 1000
991 @item 1001 @item
992 In constructs for defining or altering functions. If @code{defun} 1002 In constructs for defining or altering functions. If @code{defun}
993 were not a primitive, it could be written in Lisp (as a macro) using 1003 were not a primitive, it could be written in Lisp (as a macro) using
994 @code{fset}. 1004 @code{fset}.
995 @end itemize 1005 @end itemize
996 1006
997 Here are examples of the first two uses: 1007 Here are examples of these uses:
998 1008
999 @example 1009 @example
1000 @group 1010 @group
1001 ;; @r{Give @code{first} the same definition @code{car} has.} 1011 ;; @r{Save @code{foo}'s definition in @code{old-foo}.}
1002 (fset 'first (symbol-function 'car)) 1012 (fset 'old-foo (symbol-function 'foo))
1003 @result{} #<subr car>
1004 @end group
1005 @group
1006 (first '(1 2 3))
1007 @result{} 1
1008 @end group 1013 @end group
1009 1014
1010 @group 1015 @group
1011 ;; @r{Make the symbol @code{car} the function definition of @code{xfirst}.} 1016 ;; @r{Make the symbol @code{car} the function definition of @code{xfirst}.}
1017 ;; @r{(Most likely, @code{defalias} would be better than @code{fset} here.)}
1012 (fset 'xfirst 'car) 1018 (fset 'xfirst 'car)
1013 @result{} car 1019 @result{} car
1014 @end group 1020 @end group
1015 @group 1021 @group
1016 (xfirst '(1 2 3)) 1022 (xfirst '(1 2 3))
1028 @group 1034 @group
1029 ;; @r{Define a named keyboard macro.} 1035 ;; @r{Define a named keyboard macro.}
1030 (fset 'kill-two-lines "\^u2\^k") 1036 (fset 'kill-two-lines "\^u2\^k")
1031 @result{} "\^u2\^k" 1037 @result{} "\^u2\^k"
1032 @end group 1038 @end group
1033 @end example 1039
1034 1040 @group
1035 See also the related function @code{defalias}, in @ref{Defining 1041 ;; @r{Here is a function that alters other functions.}
1036 Functions}. 1042 (defun copy-function-definition (new old)
1043 "Define NEW with the same function definition as OLD."
1044 (fset new (symbol-function old)))
1045 @end group
1046 @end example
1037 @end defun 1047 @end defun
1038 1048
1039 When writing a function that extends a previously defined function, 1049 When writing a function that extends a previously defined function,
1040 the following idiom is sometimes used: 1050 the following idiom is sometimes used:
1041 1051
1056 defines @code{foo} rather than @code{old-foo}, it does not produce the 1066 defines @code{foo} rather than @code{old-foo}, it does not produce the
1057 proper results. The only way to avoid this problem is to make sure the 1067 proper results. The only way to avoid this problem is to make sure the
1058 file is loaded before moving aside the old definition of @code{foo}. 1068 file is loaded before moving aside the old definition of @code{foo}.
1059 1069
1060 But it is unmodular and unclean, in any case, for a Lisp file to 1070 But it is unmodular and unclean, in any case, for a Lisp file to
1061 redefine a function defined elsewhere. 1071 redefine a function defined elsewhere. It is cleaner to use the advice
1072 facility (@pxref{Advising Functions}).
1062 1073
1063 @node Inline Functions 1074 @node Inline Functions
1064 @section Inline Functions 1075 @section Inline Functions
1065 @cindex inline functions 1076 @cindex inline functions
1066 1077
1081 the size of compiled code both in files and in memory. Since the speed 1092 the size of compiled code both in files and in memory. Since the speed
1082 advantage of inline functions is greatest for small functions, you 1093 advantage of inline functions is greatest for small functions, you
1083 generally should not make large functions inline. 1094 generally should not make large functions inline.
1084 1095
1085 It's possible to define a macro to expand into the same code that an 1096 It's possible to define a macro to expand into the same code that an
1086 inline function would execute. But the macro would have a limitation: 1097 inline function would execute. (@xref{Macros}.) But the macro would be
1087 you can use it only explicitly---a macro cannot be called with 1098 limited to direct use in expressions---a macro cannot be called with
1088 @code{apply}, @code{mapcar} and so on. Also, it takes some work to 1099 @code{apply}, @code{mapcar} and so on. Also, it takes some work to
1089 convert an ordinary function into a macro. (@xref{Macros}.) To convert 1100 convert an ordinary function into a macro. To convert it into an inline
1090 it into an inline function is very easy; simply replace @code{defun} 1101 function is very easy; simply replace @code{defun} with @code{defsubst}.
1091 with @code{defsubst}. Since each argument of an inline function is 1102 Since each argument of an inline function is evaluated exactly once, you
1092 evaluated exactly once, you needn't worry about how many times the 1103 needn't worry about how many times the body uses the arguments, as you
1093 body uses the arguments, as you do for macros. (@xref{Argument 1104 do for macros. (@xref{Argument Evaluation}.)
1094 Evaluation}.)
1095 1105
1096 Inline functions can be used and open-coded later on in the same file, 1106 Inline functions can be used and open-coded later on in the same file,
1097 following the definition, just like macros. 1107 following the definition, just like macros.
1098 1108
1099 @c Emacs versions prior to 19 did not have inline functions. 1109 @c Emacs versions prior to 19 did not have inline functions.
1125 See @ref{Eval}. 1135 See @ref{Eval}.
1126 1136
1127 @item funcall 1137 @item funcall
1128 See @ref{Calling Functions}. 1138 See @ref{Calling Functions}.
1129 1139
1140 @item function
1141 See @ref{Anonymous Functions}.
1142
1130 @item ignore 1143 @item ignore
1131 See @ref{Calling Functions}. 1144 See @ref{Calling Functions}.
1132 1145
1133 @item indirect-function 1146 @item indirect-function
1134 See @ref{Function Indirection}. 1147 See @ref{Function Indirection}.
1142 @item mapatoms 1155 @item mapatoms
1143 See @ref{Creating Symbols}. 1156 See @ref{Creating Symbols}.
1144 1157
1145 @item mapcar 1158 @item mapcar
1146 See @ref{Mapping Functions}. 1159 See @ref{Mapping Functions}.
1160
1161 @item map-char-table
1162 See @ref{Char-Tables}.
1147 1163
1148 @item mapconcat 1164 @item mapconcat
1149 See @ref{Mapping Functions}. 1165 See @ref{Mapping Functions}.
1150 1166
1151 @item undefined 1167 @item undefined