diff options
| author | Chong Yidong | 2009-09-13 01:47:03 +0000 |
|---|---|---|
| committer | Chong Yidong | 2009-09-13 01:47:03 +0000 |
| commit | 5d6ab6725966988280a28cbd0acd76996a26560e (patch) | |
| tree | 7b53a16a0eec2d2aa506b0610236f8bd18dfb59a | |
| parent | adba8116c3a918f2f091600b60ea1700c9ea7362 (diff) | |
| download | emacs-5d6ab6725966988280a28cbd0acd76996a26560e.tar.gz emacs-5d6ab6725966988280a28cbd0acd76996a26560e.zip | |
* functions.texi (Anonymous Functions): Rearrange discussion,
giving usage of unquoted lambda forms first. Mention that
`function' and `#'' are no longer required (Bug#4290).
| -rw-r--r-- | doc/lispref/ChangeLog | 6 | ||||
| -rw-r--r-- | doc/lispref/functions.texi | 123 |
2 files changed, 61 insertions, 68 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 8b00b4cd360..f84d928954a 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2009-09-13 Chong Yidong <cyd@stupidchicken.com> | ||
| 2 | |||
| 3 | * functions.texi (Anonymous Functions): Rearrange discussion, | ||
| 4 | giving usage of unquoted lambda forms first. Mention that | ||
| 5 | `function' and `#'' are no longer required (Bug#4290). | ||
| 6 | |||
| 1 | 2009-09-11 Alan Mackenzie <acm@muc.de> | 7 | 2009-09-11 Alan Mackenzie <acm@muc.de> |
| 2 | 8 | ||
| 3 | * os.texi (Terminal Output): document `send-string-to-terminal' in | 9 | * os.texi (Terminal Output): document `send-string-to-terminal' in |
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi index 36a937c529a..75e4da0978d 100644 --- a/doc/lispref/functions.texi +++ b/doc/lispref/functions.texi | |||
| @@ -887,9 +887,9 @@ bool-vector, or a string. | |||
| 887 | 887 | ||
| 888 | In Lisp, a function is a list that starts with @code{lambda}, a | 888 | In Lisp, a function is a list that starts with @code{lambda}, a |
| 889 | byte-code function compiled from such a list, or alternatively a | 889 | byte-code function compiled from such a list, or alternatively a |
| 890 | primitive subr-object; names are ``extra.'' Although usually functions | 890 | primitive subr-object; names are ``extra.'' Although functions are |
| 891 | are defined with @code{defun} and given names at the same time, it is | 891 | usually defined with @code{defun} and given names at the same time, it |
| 892 | occasionally more concise to use an explicit lambda expression---an | 892 | is occasionally more concise to use an explicit lambda expression---an |
| 893 | anonymous function. Such a list is valid wherever a function name is. | 893 | anonymous function. Such a list is valid wherever a function name is. |
| 894 | 894 | ||
| 895 | Any method of creating such a list makes a valid function. Even this: | 895 | Any method of creating such a list makes a valid function. Even this: |
| @@ -916,17 +916,21 @@ makes it the value (@emph{not} the function definition!) of | |||
| 916 | @end example | 916 | @end example |
| 917 | 917 | ||
| 918 | @noindent | 918 | @noindent |
| 919 | (It does @emph{not} work to write @code{(silly 1)}, because this function | 919 | It does @emph{not} work to write @code{(silly 1)}, because this |
| 920 | is not the @emph{function definition} of @code{silly}. We have not given | 920 | function is not the @emph{function definition} of @code{silly}. We |
| 921 | @code{silly} any function definition, just a value as a variable.) | 921 | have not given @code{silly} any function definition, just a value as a |
| 922 | variable. | ||
| 922 | 923 | ||
| 923 | Most of the time, anonymous functions are constants that appear in | 924 | Most of the time, anonymous functions are constants that appear in |
| 924 | your program. For example, you might want to pass one as an argument to | 925 | your program. For instance, you might want to pass one as an argument |
| 925 | the function @code{mapcar}, which applies any given function to each | 926 | to the function @code{mapcar}, which applies any given function to |
| 926 | element of a list. | 927 | each element of a list (@pxref{Mapping Functions}). |
| 928 | @xref{describe-symbols example}, for a realistic example of this. | ||
| 927 | 929 | ||
| 928 | Here we define a function @code{change-property} which | 930 | In the following example, we define a @code{change-property} |
| 929 | uses a function as its third argument: | 931 | function that takes a function as its third argument, followed by a |
| 932 | @code{double-property} function that makes use of | ||
| 933 | @code{change-property} by passing it an anonymous function: | ||
| 930 | 934 | ||
| 931 | @example | 935 | @example |
| 932 | @group | 936 | @group |
| @@ -934,83 +938,48 @@ uses a function as its third argument: | |||
| 934 | (let ((value (get symbol prop))) | 938 | (let ((value (get symbol prop))) |
| 935 | (put symbol prop (funcall function value)))) | 939 | (put symbol prop (funcall function value)))) |
| 936 | @end group | 940 | @end group |
| 937 | @end example | ||
| 938 | |||
| 939 | @noindent | ||
| 940 | Here we define a function that uses @code{change-property}, | ||
| 941 | passing it a function to double a number: | ||
| 942 | 941 | ||
| 943 | @example | ||
| 944 | @group | ||
| 945 | (defun double-property (symbol prop) | ||
| 946 | (change-property symbol prop '(lambda (x) (* 2 x)))) | ||
| 947 | @end group | ||
| 948 | @end example | ||
| 949 | |||
| 950 | @noindent | ||
| 951 | In such cases, we usually use the special form @code{function} instead | ||
| 952 | of simple quotation to quote the anonymous function, like this: | ||
| 953 | |||
| 954 | @example | ||
| 955 | @group | 942 | @group |
| 956 | (defun double-property (symbol prop) | 943 | (defun double-property (symbol prop) |
| 957 | (change-property symbol prop | 944 | (change-property symbol prop (lambda (x) (* 2 x)))) |
| 958 | (function (lambda (x) (* 2 x))))) | ||
| 959 | @end group | 945 | @end group |
| 960 | @end example | 946 | @end example |
| 961 | 947 | ||
| 962 | Using @code{function} instead of @code{quote} makes a difference if you | ||
| 963 | compile the function @code{double-property}. For example, if you | ||
| 964 | compile the second definition of @code{double-property}, the anonymous | ||
| 965 | function is compiled as well. By contrast, if you compile the first | ||
| 966 | definition which uses ordinary @code{quote}, the argument passed to | ||
| 967 | @code{change-property} is the precise list shown: | ||
| 968 | |||
| 969 | @example | ||
| 970 | (lambda (x) (* x 2)) | ||
| 971 | @end example | ||
| 972 | |||
| 973 | @noindent | 948 | @noindent |
| 974 | The Lisp compiler cannot assume this list is a function, even though it | 949 | In the @code{double-property} function, we did not quote the |
| 975 | looks like one, since it does not know what @code{change-property} will | 950 | @code{lambda} form. This is permissible, because a @code{lambda} form |
| 976 | do with the list. Perhaps it will check whether the @sc{car} of the third | 951 | is @dfn{self-quoting}: evaluating the form yields the form itself. |
| 977 | element is the symbol @code{*}! Using @code{function} tells the | ||
| 978 | compiler it is safe to go ahead and compile the constant function. | ||
| 979 | 952 | ||
| 980 | Nowadays it is possible to omit @code{function} entirely, like this: | 953 | Whether or not you quote a @code{lambda} form makes a difference if |
| 954 | you compile the code (@pxref{Byte Compilation}). If the @code{lambda} | ||
| 955 | form is unquoted, as in the above example, the anonymous function is | ||
| 956 | also compiled. Suppose, however, that we quoted the @code{lambda} | ||
| 957 | form: | ||
| 981 | 958 | ||
| 982 | @example | 959 | @example |
| 983 | @group | 960 | @group |
| 984 | (defun double-property (symbol prop) | 961 | (defun double-property (symbol prop) |
| 985 | (change-property symbol prop (lambda (x) (* 2 x)))) | 962 | (change-property symbol prop '(lambda (x) (* 2 x)))) |
| 986 | @end group | 963 | @end group |
| 987 | @end example | 964 | @end example |
| 988 | 965 | ||
| 989 | @noindent | 966 | @noindent |
| 990 | This is because @code{lambda} itself implies @code{function}. | 967 | If you compile this, the argument passed to @code{change-property} is |
| 991 | 968 | the precise list shown: | |
| 992 | We sometimes write @code{function} instead of @code{quote} when | ||
| 993 | quoting the name of a function, but this usage is just a sort of | ||
| 994 | comment: | ||
| 995 | 969 | ||
| 996 | @example | 970 | @example |
| 997 | (function @var{symbol}) @equiv{} (quote @var{symbol}) @equiv{} '@var{symbol} | 971 | (lambda (x) (* x 2)) |
| 998 | @end example | ||
| 999 | |||
| 1000 | @cindex @samp{#'} syntax | ||
| 1001 | The read syntax @code{#'} is a short-hand for using @code{function}. | ||
| 1002 | For example, | ||
| 1003 | |||
| 1004 | @example | ||
| 1005 | #'(lambda (x) (* x x)) | ||
| 1006 | @end example | 972 | @end example |
| 1007 | 973 | ||
| 1008 | @noindent | 974 | @noindent |
| 1009 | is equivalent to | 975 | The Lisp compiler cannot assume this list is a function, even though |
| 976 | it looks like one, since it does not know what @code{change-property} | ||
| 977 | will do with the list. Perhaps it will check whether the @sc{car} of | ||
| 978 | the third element is the symbol @code{*}! | ||
| 1010 | 979 | ||
| 1011 | @example | 980 | @findex function |
| 1012 | (function (lambda (x) (* x x))) | 981 | The @code{function} special form explicitly tells the byte-compiler |
| 1013 | @end example | 982 | that its argument is a function: |
| 1014 | 983 | ||
| 1015 | @defspec function function-object | 984 | @defspec function function-object |
| 1016 | @cindex function quoting | 985 | @cindex function quoting |
| @@ -1021,8 +990,26 @@ to be used only as a function, and therefore can safely be compiled. | |||
| 1021 | Contrast this with @code{quote}, in @ref{Quoting}. | 990 | Contrast this with @code{quote}, in @ref{Quoting}. |
| 1022 | @end defspec | 991 | @end defspec |
| 1023 | 992 | ||
| 1024 | @xref{describe-symbols example}, for a realistic example using | 993 | @cindex @samp{#'} syntax |
| 1025 | @code{function} and an anonymous function. | 994 | The read syntax @code{#'} is a short-hand for using @code{function}. |
| 995 | Generally, it is not necessary to use either @code{#'} or | ||
| 996 | @code{function}; just use an unquoted @code{lambda} form instead. | ||
| 997 | (Actually, @code{lambda} is a macro defined using @code{function}.) | ||
| 998 | The following forms are all equivalent: | ||
| 999 | |||
| 1000 | @example | ||
| 1001 | #'(lambda (x) (* x x)) | ||
| 1002 | (function (lambda (x) (* x x))) | ||
| 1003 | (lambda (x) (* x x)) | ||
| 1004 | @end example | ||
| 1005 | |||
| 1006 | We sometimes write @code{function} instead of @code{quote} when | ||
| 1007 | quoting the name of a function, but this usage is just a sort of | ||
| 1008 | comment: | ||
| 1009 | |||
| 1010 | @example | ||
| 1011 | (function @var{symbol}) @equiv{} (quote @var{symbol}) @equiv{} '@var{symbol} | ||
| 1012 | @end example | ||
| 1026 | 1013 | ||
| 1027 | @node Function Cells | 1014 | @node Function Cells |
| 1028 | @section Accessing Function Cell Contents | 1015 | @section Accessing Function Cell Contents |