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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.
Интервал:
Закладка:
Another thing to note about DEFCONSTANT
is that while the language allows you to redefine a constant by reevaluating a DEFCONSTANT
with a different initial-value-form, what exactly happens after the redefinition isn't defined. In practice, most implementations will require you to reevaluate any code that refers to the constant in order to see the new value since the old value may well have been inlined. Consequently, it's a good idea to use DEFCONSTANT
only to define things that are really constant, such as the value of NIL. For things you might ever want to change, you should use DEFPARAMETER
instead.
Assignment
Once you've created a binding, you can do two things with it: get the current value and set it to a new value. As you saw in Chapter 4, a symbol evaluates to the value of the variable it names, so you can get the current value simply by referring to the variable. To assign a new value to a binding, you use the SETF
macro, Common Lisp's general-purpose assignment operator. The basic form of SETF
is as follows:
(setf place value )
Because SETF
is a macro, it can examine the form of the place it's assigning to and expand into appropriate lower-level operations to manipulate that place. When the place is a variable, it expands into a call to the special operator SETQ
, which, as a special operator, has access to both lexical and dynamic bindings. [81] Some old-school Lispers prefer to use SETQ with variables, but modern style tends to use SETF for all assignments.
For instance, to assign the value 10 to the variable x
, you can write this:
(setf x 10)
As I discussed earlier, assigning a new value to a binding has no effect on any other bindings of that variable. And it doesn't have any effect on the value that was stored in the binding prior to the assignment. Thus, the SETF
in this function:
(defun foo (x) (setf x 10))
will have no effect on any value outside of foo
. The binding that was created when foo
was called is set to 10, immediately replacing whatever value was passed as an argument. In particular, a form such as the following:
(let ((y 20))
(foo y)
(print y))
will print 20, not 10, as it's the value of y
that's passed to foo
where it's briefly the value of the variable x
before the SETF
gives x
a new value.
SETF
can also assign to multiple places in sequence. For instance, instead of the following:
(setf x 1)
(setf y 2)
you can write this:
(setf x 1 y 2)
SETF
returns the newly assigned value, so you can also nest calls to SETF
as in the following expression, which assigns both x
and y
the same random value:
(setf x (setf y (random 10)))
Generalized Assignment
Variable bindings, of course, aren't the only places that can hold values. Common Lisp supports composite data structures such as arrays, hash tables, and lists, as well as user-defined data structures, all of which consist of multiple places that can each hold a value.
I'll cover those data structures in future chapters, but while we're on the topic of assignment, you should note that SETF
can assign any place a value. As I cover the different composite data structures, I'll point out which functions can serve as " SETF
able places." The short version, however, is if you need to assign a value to a place, SETF
is almost certainly the tool to use. It's even possible to extend SETF
to allow it to assign to user-defined places though I won't cover that. [82] Look up DEFSETF , DEFINE-SETF-EXPANDER for more information.
In this regard SETF
is no different from the =
assignment operator in most C-derived languages. In those languages, the =
operator assigns new values to variables, array elements, and fields of classes. In languages such as Perl and Python that support hash tables as a built-in data type, =
can also set the values of individual hash table entries. Table 6-1 summarizes the various ways =
is used in those languages.
Table 6-1. Assignment with =
in Other Languages
Assigning to ... | Java, C, C++ | Perl | Python |
... variable | x = 10; |
$x = 10; |
x = 10 |
... array element | a[0] = 10; |
$a[0] = 10; |
a[0] = 10 |
... hash table entry | — |
$hash{'key'} = 10; |
hash['key'] = 10 |
... field in object | o.field = 10; |
$o->{'field'} = 10; |
o.field = 10 |
SETF
works the same way—the first "argument" to SETF
is a place to store the value, and the second argument provides the value. As with the =
operator in these languages, you use the same form to express the place as you'd normally use to fetch the value. [83] The prevalence of Algol-derived syntax for assignment with the "place" on the left side of the = and the new value on the right side has spawned the terminology lvalue , short for "left value," meaning something that can be assigned to, and rvalue , meaning something that provides a value. A compiler hacker would say, " SETF treats its first argument as an lvalue."
Thus, the Lisp equivalents of the assignments in Table 6-1—given that AREF
is the array access function, GETHASH
does a hash table lookup, and field
might be a function that accesses a slot named field
of a user-defined object—are as follows:
Simple variable: (setf x 10)
Array: (setf (aref a 0) 10)
Hash table: (setf (gethash 'key hash) 10)
Slot named 'field': (setf (field o) 10)
Note that SETF
ing a place that's part of a larger object has the same semantics as SETF
ing a variable: the place is modified without any effect on the object that was previously stored in the place. Again, this is similar to how =
behaves in Java, Perl, and Python. [84] C programmers may want to think of variables and other places as holding a pointer to the real object; assigning to a variable simply changes what object it points to while assigning to a part of a composite object is similar to indirecting through the pointer to the actual object. C++ programmers should note that the behavior of = in C++ when dealing with objects—namely, a memberwise copy—is quite idiosyncratic.
Other Ways to Modify Places
Интервал:
Закладка:
Похожие книги на «Practical Common Lisp»
Представляем Вашему вниманию похожие книги на «Practical Common Lisp» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.
Обсуждение, отзывы о книге «Practical Common Lisp» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.