Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
MacLennan- Chap. 9
In the 1950s Newell, Shaw, and Simon (at Carnegie institute of technology and the Rand Corporation) developed many of the ideas of list processing in the IPL family of programming languages. these ideas included the linked representation of list structures and the use of a stack (specifically, a push-down list) to implement recursion.
In the summer of 1956, the first major workshop on artificial intelligence was held at Dartmouth. At this workshop john McCarthy, then at MIT, heard a description of the ipl2 programming language. which had a low-level pseudo-code, or assembly-language-like syntax. McCarthy realized that an algebraic listprocessing language, on the style of the recently announced FORTRAN I system, would be very useful.
6
An important consequence of this invention was that it made it feasible to compose IF function and the listprocessing functions to achieve more complicated actions.
Since FORTRAN does not permit recursive definitions, it became apparent that a new language was needed. In 1958 McCarthy began using recursion in conjunction with conditional expression in his definition of list-processing functions.
9
He defined a universal LISP function that could interpret any other lisp function. he wrote a LISP interpreter in LISP. Since manipulates only lists, writing a universal function required developing a way of representing LISP program as list structures.
12
For example the function call f [x+y; u*z] Would be represented by a list whose first element is f and whose second and third element are the lists representing x+y and u*z. in LISP this list is written as (f (plus x y) (times u z) )
13
The algol-like notation (e,g., f[x+y; u*z]) is called M-expressions (M for meta-language). The list notation is called Sexpressions (S for symbolic language)
14
15
16
In the LISP almost everything is a function application (f a1 a2an) where f is the function and a1, a2, , an are the arguments. This notation is called Combridge Polish (prefix notation). Example: (plus 2 3) (plus 10 5 8 64) LISP is fully parenthesized
18
Example
(set Freq (make-table text nil)) is a nested function application : set is applied to two arguments: Freq the result of applying make-table to the arguments text and nil. In an Algol-like language this would be written set (Freq, make-table (text, nil))
19
Example
(cond ((null x) 0) ((eq x y) (f x)) (t (g y)) ) In an Algol-like language this would be written If null(x) then 0 elseif x = y then f(x) else g(y) endif
20
function definition is accomplished by calling a function, defun, with three arguments: the name of the function, its formal parameter list, and its body.
21
22
LISP manipulates list just like other languages manipulate number; they can be compared, passed to functions, put together, and taken apart.
24
If the list is quoted, then it is treated as data; that is, it is unevaluated. Example: (set text (to be or not to be)) (set text (to be or not to be)) What happens?
26
the fact that LISP represents both programs and data in the same way is of the utmost importance: it makes it very easy to write a LISP interpreter in LISP. it makes it convenient to have one LISP program generate and call for the execution of another LISP program. It also simplifies writing LISP programs that transform and manipulate other LISP programs.
27
These capabilities are important in artificial intelligence and other advanced programdevelopment environments. These amplification come with a reduction of readability.
28
Pure Function
functions like eq and plus are pure functions (or simply functions) because they have no effect other than the computation of a value. Pure functions obey the Manifest Interface Principle because their interfaces are apparent (manifest).
30
31
Procedures
Some function in LISP are pseudo-function (or procedure). These are functions that have a side effect on the state of computer in addition to computing a result. Example: (set text (to be or not to be)) binds the name (atom) text to the (to be or not to be) that it is a list and return this list. The atom n can now be used as a name for this list in any expression, for example (set Freq (make-table text nil))
32
Function
another important pseudo-function is defun, which define a function. Example: (defun f (x1,x2, , xn) b) defines a function with the name f, formal parameters x1, x2, , xn; and body b The function application used to define functions is different in many dialects.
in LISP the binding process is dynamic, that is, it takes place at run-time.
33
34
There are at least two types of atoms in all LISP systems: Numeric Nonnumeric
36
LISP provides a very large set of primitive functions for manipulating numeric atoms: Arithmetic operations (plus, difference, etc.) Predecessor and successor function (sub1, add1) Maximum and minimum functions Relation tests (equal, lessp, greaterp) Predicates (i.e., tests, such as zerop, onep, minusp)
37
All of these functions take both integer and floating-point (and in some systems, multiple-precision) arguments and return results of the appropriate type.
38
Example
LISPs use of Combridge Polish limits its numerical applications. Example: (-b + (b - 4ac) ) /2a must be written (quotient (plus (minus b) (sqrt (difference (expt b 2) (times 4 a c)))) (times 2 a)
39
These atoms are strings of characters that were originally intended to represent words or symbols.
40
Example
With few exceptions, the only operations that can be performed on nonnumeric atoms are comparisions for equality and inequality. This is done with function eq: Example: (eq x y) t if x and y are the same atom, nil if x and y are not the same atom,
41
the atom nil has many uses in LISP. This operation is often used as a base for recursive definitions. (eq x nil) (null x) nil is the noun and null is the corresponding adjective.
42
Some LISP systems provide additional types of atoms, such as strings. In these cases special operations for manipulating these values are also provided
43
An abstract data type is a set of data values together with a set of operations on those values.
44
The empty list, ( ) is considered equivalent to the atom nil: (eq ( ) nil) or (null ( ) ) Except the null list all lists are nonatomic, they are sometimes called composite data values.
46
We can find out whether or not something is an atom by using the atom predicate. Example: (atom to) t (atom (plus 2 3)) t (atom nil) t (atom (to be) ) nil (atom ( ( ) ) ) nil Notice neither ( ( ) ) nor (nil) is the null list
47
48
Notice that the argument to car and cdr is always a nonnull list. Unlike car, cdr always returns a list. Car and Cdr are pure functions. The easiest way to think of the way they work is that they make a new copy of the list.
50
Car and Cdr can be used in combination to access the components of a list
Example: (set DS (Don Smith) 45 (august 25 1980) ) ) (car (cdr (cdr (cdr DS)))) returns the list (august 25 1980)
51
30000
In general, the nth element of a list can be access by n-1 cdrs followed by a car. The composition of cars and cdrs is represented by the sequence of as and ds between the initial c and the final r. This can be seen more clearly if the list is written as a linked data structure; then a d moves to the right and an a moves down.
52
Figure 9.2
53
Clearly, these sequence of as and ds can become quite complicated to read. Writing them is also error-prone. One solution to this is to write a set of special-purpose functions for accessing the part of a record. For example a function for accessing the hire date could be defined as (defun hire-date (r) (cadddr)) Then (hire-date DS) returns Don Smith hire date
54
56
What will this function do if we ask for a property that is not in the property list? (getprop weight DS) Error: car of nil
57
One way to do this is to have getprop return a distinguished value if the property does not exist. An obvious choice is nil,
but this would not be a good choice since it would then be impossible to distinguish an undefined property from one that is defined but whose value is nil.
58
A better decision is to pick some atom, such as undefined-property, which is unlikely to be used for any other purpose. (getprop weight DS) undefined-property
59
These problems are solved by another common LISP data structure, the association list, or a-list. An a-list is a list of pairs. The general form of an a-list is a list of attributevalue pairs: ((a1 v1) (a2 v2)(an vn))
( (name (Don Smith) (age 45) (salary 30000) (hire-date (August 25 1980)) )
61
Assoc
The function that does the forward association. For example (assoc hire-date DS ) Return (August 25 1980)
62
63
Example: (cons to (be or not to be)) return the list (to be or not to be) (car (to be or not to be)) = (cdr (to be or not to be)) = be)) (cons to (be or not to be)) = be)
65
Notice that the second argument of cons must be a list. Cons is a pure function.
66
67
Append
(append M L) will return the concatenation of the lists L and M. (append ( ) L) = L (append L ( )) = L (append (b c) (d e f)) = (b c d e f)
68
LISP was originally developed for artificial intelligence application. AI applications often must deal with
the properties of objects and the relationship among objects.
69
In LISP,
objects are represented by atoms, each atom has an associated p-list
represents the properties of the atom and the relationship in which it participates.
70
(set Europe (England France Spain Germany )) >> (England France Spain Germany ) (car Europe) >> England (eq England (car Europe)) >> t (eq France (car Europe)) >> nil
71
Return Paris We can find out the value of a property with the get function: (get France capital) Return Paris Figure 9.7
72
Figure 9.7
73
Each object comes complete with a property, its print name, which is a character string tagged by pname.
74
75
Figure 9.13
76
Binding atoms to lists: (Set Europe (England France ) (get Europe apval) (England France Spain )
77
Binding of atoms to functions is represented by the expr property (Get getprop expr) {function expr) Defun equivalent to putprop
78
Figure 9.8
80
List containing other lists are represented in the same way. For example, the list ( (to 2) (be 2) (or 1) (not 1) ) would be represented in storage as shown in below.
81
Figure 9.9
82
83
Figure 9.10
84
85
Because car,cdr,cons are pure functions, they have no side effect on memory, so they are danger free
87
(rplacd L D)
L^ .right := D;
88
Figure 9.12
(set text (to be or not to be)) >> (to be or not to be) ((rplacd (cdr text) (is all))
89