Previous | Table of Contents | Next |
Here is a very simple function that is interactive, takes an argument, and presents a message to the user:
(defun multiply-by-seven (number) Multiply NUMBER by seven. (interactive p) (message The result is %d (* 7 number)))
This function multiplies the number that is presented to it by seven and displays the answer to the user.
It follows the five-part template described previously.
After the symbol defun is the name of the definition, multiply-by-seven. Next is the argument list. In this definition, the list consists of one element, number. It is good practice to name arguments so they are more or less self-explanatory. You could use any symbol you want, such as quoox, but such a symbol, although perfectly clear to the computer, which does not recognize meaning, would be obscure to a human who searches for meaning.
After the argument list, starting on a line of its own (and indented two spaces), is the documentation. This documentation fits all on one line so you can read it all when you use the apropos or a similar command. By convention, the definitions argument is shown in uppercase letters so you can recognize it readily.
The third line is the interactive specification. It not only contains the symbol interactive but also an argument to that symbol, p. That argument is one of more than 20 alternative arguments that specify to the definition how information is passed to the function. In this instance, a lowercase p tells the Lisp interpreter that information will be passed to the function as a prefix argument that is converted to a number.
Thus, to use multiply-by-seven to multiply 5 by 7, you first evaluate the definition, and then type
C-u 5 M-x multiply-by-seven RET
where 5 is the prefix argument, introduced by C-u.
If you do this inside Emacs, you see
The result is 35
in the echo area.
Back to an examination of the function definition.
The body of the definition, the fifth part, is the single line
(message The result is %d (* 7 number)))
This line is a list. The first element of the list is the function message.
If you examine the documentation string for message, for example, by typing C-h f message RET, you find that it says, in part:
Print a one-line message at the bottom of the screen. The first argument is a format control string, and the rest are data to be formatted under control of the string. See format for details.
In this case, the first argument to message is The result is %d. message displays The result is as written; and it substitutes the value computed by the second argument into the format specification, which is %d.
The second argument is the list (* 7 number)). This list looks very like the first example of adding two plus two! Indeed, it is very similar, except that instead of +, it uses *, which is the symbol for multiplication. 7 is a number and number is the local variable declared on the first line in the argument list to the function definition. When you interactively call multiply-by-seven with the prefix argument 5, number takes on that value, so the list (* 7 number)) becomes (* 7 5)). This list is evaluated, resulting in 35, which is passed to the format specification, %d.
35 replaces %d so the message you see at the bottom of your screen is
The result is 35
In Emacs Lisp, a symbol can have a value attached to it just as it can have a function definition attached to it. The two are different. The function definition is a set of instructions that a computer will obey. A value, on the other hand, is something, such as number or a name, that can vary (which is why such a symbol is called a variable). The value of a symbol can be any expression in Lisp, such as a symbol, number, list, or string. (Also, at the same time, a symbol can have two other things attached to it: a property list and its name.)
The following simple program sets the value of the symbol foo to 5 (it introduces the concepts of value and variable)6:
6foo is what might be called a conventional, meaningless symbol. It is often used as a nonce variable or as the name of a scratch file. foo is the first of a series of such-like metasyntactic variables, the second of which is bar.
(setq foo 5)
In this instance, the symbol foo is being perceived as a variable, that is, as a symbol that holds a valuein this case 5that can be varied.
We can change the value of foo from 5 to 7 by evaluating this expression:
(setq foo 7)
After this expression is evaluated, the value of foo is 7. setq is a function that sets the first argument to the value of the second argument.
A variable need not be a number; it can be a string of letters. Such a string must be enclosed in double quotation marks, which tell the Lisp interpreter that the letters are a string.
For example, we can make the value of foo be the string of letters Hello, world by evaluating the following expression:
(setq foo Hello, world)
To determine the value of a variable, simply evaluate the variable on its own, not inside parentheses; place your cursor immediately after the variable and type C-x C-e:
foo → Hello, world
where → means evaluates to and points to the value returned to by the expression being evaluated.
Previous | Table of Contents | Next |