Peter Siebel - Practical Common Lisp
Здесь есть возможность читать онлайн «Peter Siebel - Practical Common Lisp» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Год выпуска: 2005, ISBN: 2005, Издательство: Apress, Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.
- Название:Practical Common Lisp
- Автор:
- Издательство:Apress
- Жанр:
- Год:2005
- ISBN:1-59059-239-5
- Рейтинг книги:4 / 5. Голосов: 1
-
Избранное:Добавить в избранное
- Отзывы:
-
Ваша оценка:
- 80
- 1
- 2
- 3
- 4
- 5
Practical Common Lisp: краткое содержание, описание и аннотация
Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Practical Common Lisp»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.
Practical Common Lisp — читать онлайн бесплатно полную книгу (весь текст) целиком
Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Practical Common Lisp», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.
Интервал:
Закладка:
Thus, a DEFUN
appearing at the top level of a source file is a top-level form—the code that defines the function and associates it with its name will run when the FASL is loaded—but the forms within the body of the function, which won't run until the function is called, aren't top-level forms. Most forms are compiled the same when compiled as top-level and non-top-level forms, but the semantics of an EVAL-WHEN
depend on whether it's being compiled as a top- level form, compiled as a non-top-level form, or simply evaluated, combined with what situations are listed in its situation list.
The situations :compile-toplevel
and :load-toplevel
control the meaning of an EVAL-WHEN
compiled as a top-level form. When :compile-toplevel
is present, the file compiler will evaluate the subforms at compile time. When :load-toplevel
is present, it will compile the subforms as top-level forms. If neither of these situations is present in a top-level EVAL-WHEN
, the compiler ignores it.
When an EVAL-WHEN
is compiled as a non-top-level form, it's either compiled like a PROGN
, if the :execute
situation is specified, or ignored. Similarly, an evaluated EVAL-WHEN
—which includes top-level EVAL-WHEN
s in a source file processed by LOAD
and EVAL-WHEN
s evaluated at compile time because they appear as subforms of a top-level EVAL-WHEN
with the :compile-toplevel
situation—is treated like a PROGN
if :execute
is present and ignored otherwise.
Thus, a macro such as IN-PACKAGE
can have the necessary effect at both compile time and when loading from source by expanding into an EVAL-WHEN
like the following:
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf *package* (find-package "PACKAGE-NAME")))
*PACKAGE*
will be set at compile time because of the :compile-toplevel
situation, set when the FASL is loaded because of :load-toplevel
, and set when the source is loaded because of the :execute
.
There are two ways you're most likely to use EVAL-WHEN
. One is if you want to write macros that need to save some information at compile time to be used when generating the expansion of other macro forms in the same file. This typically arises with definitional macros where a definition early in a file can affect the code generated for a definition later in the same file. You'll write this kind of macro in Chapter 24.
The other time you might need EVAL-WHEN
is if you want to put the definition of a macro and helper functions it uses in the same file as code that uses the macro. DEFMACRO
already includes an EVAL-WHEN
in its expansion so the macro definition is immediately available to be used later in the file. But DEFUN
normally doesn't make function definitions available at compile time. But if you use a macro in the same file as it's defined in, you need the macro and any functions it uses to be defined. If you wrap the DEFUN
s of any helper functions used by the macro in an EVAL-WHEN
with :compile-toplevel
, the definitions will be available when the macro's expansion function runs. You'll probably want to include :load-toplevel
and :execute
as well since the macros will also need the function definitions after the file is compiled and loaded or if you load the source instead of compiling.
Other Special Operators
The four remaining special operators, LOCALLY
, THE
, LOAD-TIME-VALUE
, and PROGV
, all allow you to get at parts of the underlying language that can't be accessed any other way. LOCALLY
and THE
are part of Common Lisp's declaration system, which is used to communicate things to the compiler that don't affect the meaning of your code but that may help the compiler generate better code—faster, clearer error messages, and so on. [220] The one declaration that has an effect on the semantics of a program is the SPECIAL declaration mentioned in Chapter 6.
I'll discuss declarations briefly in Chapter 32.
The other two, LOAD-TIME-VALUE
and PROGV
, are infrequently used, and explaining the reason why you might ever want to use them would take longer than explaining what they do. So I'll just tell you what they do so you know they're there. Someday you'll hit on one of those rare times when they're just the thing, and then you'll be ready.
LOAD-TIME-VALUE
is used, as its name suggests, to create a value that's determined at load time. When the file compiler compiles code that contains a LOAD-TIME-VALUE
form, it arranges to evaluate the first subform once, when the FASL is loaded, and for the code containing the LOAD-TIME-VALUE
form to refer to that value. In other words, instead of writing this:
(defvar *loaded-at* (get-universal-time))
(defun when-loaded () *loaded-at*)
you can write the following:
(defun when-loaded () (load-time-value (get-universal-time)))
In code not processed by COMPILE-FILE
, LOAD-TIME-VALUE
is evaluated once when the code is compiled, which may be when you explicitly compile a function with COMPILE
or earlier because of implicit compilation performed by the implementation in the course of evaluating the code. In uncompiled code, LOAD-TIME-VALUE
evaluates its form each time it's evaluated.
Finally, PROGV
creates new dynamic bindings for variables whose names are determined at runtime. This is mostly useful for implementing embedded interpreters for languages with dynamically scoped variables. The basic skeleton is as follows:
(progv symbols-list values-list
body-form *)
where symbols-list is a form that evaluates to a list of symbols and values-list is a form that evaluates to a list of values. Each symbol is dynamically bound to the corresponding value, and then the body-forms are evaluated. The difference between PROGV
and LET
is that because symbols-list is evaluated at runtime, the names of the variables to bind can be determined dynamically. As I say, this isn't something you need to do often.
And that's it for special operators. In the next chapter, I'll get back to hard-nosed practical topics and show you how to use Common Lisp's package system to take control of your namespaces so you can write libraries and applications that can coexist without stomping on each other's names.
21. Programming in the Large: Packages and Symbols
In Chapter 4 I discussed how the Lisp reader translates textual names into objects to be passed to the evaluator, representing them with a kind of object called a symbol . It turns out that having a built-in data type specifically for representing names is quite handy for a lot of kinds of programming. [221] The kind of programming that relies on a symbol data type is called, appropriately enough, symbolic computation. It's typically contrasted to numeric programming. An example of a primarily symbolic program that all programmers should be familiar with is a compiler—it treats the text of a program as symbolic data and translates it into a new form.
That, however, isn't the topic of this chapter. In this chapter I'll discuss one of the more immediate and practical aspects of dealing with names: how to avoid name conflicts between independently developed pieces of code.
Интервал:
Закладка:
Похожие книги на «Practical Common Lisp»
Представляем Вашему вниманию похожие книги на «Practical Common Lisp» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.
Обсуждение, отзывы о книге «Practical Common Lisp» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.