Книга: Learning GNU Emacs, 3rd Edition

11.1.1 Basic Lisp Entities

11.1.1 Basic Lisp Entities

The basic elements in Lisp you need to be familiar with are functions, variables, and atoms. Functions are the only program units in Lisp; they cover the notions of procedures, subroutines, programs, and even operators in other languages.

Functions are defined as lists of the above entities, usually as lists of calls to other, existing functions. All functions have return values (as with Perl functions and non-void Java methods); a function's return value is simply the value of the last item in the list, usually the value returned by the last function called. A function call within another function is equivalent to a statement in other languages, and we use statement interchangeably with function call in this chapter. Here is the syntax for function:

(function-name argument1 argument2 ...)

which is equivalent to this:

method_name (argument1, argument2, ...);

in Java. This syntax is used for all functions, including those equivalent to arithmetic or comparison operators in other languages. For example, in order to add 2 and 4 in Java or Perl, you would use the expression 2 + 4, whereas in Lisp you would use the following:

(+ 2 4)

Similarly, where you would use 4 >= 2 (greater than or equal to), the Lisp equivalent is:

(>= 4 2)

Variables in Lisp are similar to those in any other language, except that they do not have types. A Lisp variable can assume any type of value (values themselves do have types, but variables don't impose restrictions on what they can hold).

Atoms are values of any type, including integers, floating point (real) numbers, characters, strings, Boolean truth values, symbols, and special Emacs types such as buffers, windows, and processes. The syntax for various kinds of atoms is:

• Integers are what you would expect: signed whole numbers in the range -227 to 227- 1.

• Floating point numbers are real numbers that you can represent with decimal points and scientific notation (with lowercase "e" for the power of 10). For example, the number 5489 can be written 5489, 5.489e3, 548.9e1, and so on.

• Characters are preceded by a question mark, for example, ?a. Esc, Newline, and Tab are abbreviated e, n, and t respectively; other control characters are denoted with the prefix C-, so that (for example) C-a is denoted as ?C-a.[75]

• Strings are surrounded by double quotes; quote marks and backslashes within strings need to be preceded by a backslash. For example, "Jane said, "See Dick run."" is a legal string. Strings can be split across multiple lines without any special syntax. Everything until the closing quote, including all the line breaks, is part of the string value.

• Booleans use t for true and nil for false, though most of the time, if a Boolean value is expected, any non-nil value is assumed to mean true. nil is also used as a null or nonvalue in various situations, as we will see.

• Symbols are names of things in Lisp, for example, names of variables or functions. Sometimes it is important to refer to the name of something instead of its value, and this is done by preceding the name with a single quote ('). For example, the define-key function, described in Chapter 10, uses the name of the command (as a symbol) rather than the command itself.

A simple example that ties many of these basic Lisp concepts together is the function setq.[76] As you may have figured out from previous chapters, setq is a way of assigning values to variables, as in

(setq auto-save-interval 800)

Notice that setq is a function, unlike in other languages in which special syntax such as = or := is used for assignment. setq takes two arguments: a variable name and a value. In this example, the variable auto-save-interval (the number of keystrokes between auto-saves) is set to the value 800.

setq can actually be used to assign values to multiple variables, as in

(setq thisvar thisvalue
     thatvar thatvalue
     theothervar theothervalue)

The return value of setq is simply the last value assigned, in this case theothervalue. You can set the values of variables in other ways, as we'll see, but setq is the most widely applicable.

Оглавление книги


Генерация: 0.145. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз