Вы находитесь на странице: 1из 93

Gorod@iis.nsk.

su

, 2005


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1. . . . . . . . . . . .
2. . . . . . . . . . . . . . . . . . . . . . . . . . .
3. . . . . . . . . . . . . . . . . . . . . . . . . . . .
4. . . . . . . . . . . . . . . . . . . . . . . . . .
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6. . . . . . . . . . . . . . . . .
7. . . . . . . . . . . . . . . . . . . . . . . . .
8. . . . . . . . . . . . . . . . . . . . . .
9. . . . . . . . . . . . .
10. . . . . . . . . . . . . . .

5
10
20
32
43
51
64
76
80
89

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95


2004 .
.
PF4, PL7, PL5 CC2001CS.



. :

.
.
.
.

-,
.
,
. p (1958)
(-) 20-
. -
,
.

.
: ,
- .

. . . .
,
:
{1 1 = 0 ; ( n +1 ) -1 = n } ,
. . 1932 :
{ F (x, y, z) = (x = 1) 0

((y +1) = x) z
F (x, y +1, z +1) ;
n 1 = F (n, 0, 0) }
F ( x, y, z) x, y, z

(x = 1)
:= 0
3

(y +1) /= x
:= F (x, y +1, z +1)

N-1 ( N) N := F (N, 0, 0)

,
.


,
.
-
,
.
, ,
. ,
,
- ,

.
[5,6].

1.
,
,
.
, , :
-

.
.
.
.

1. ""
, , .
,
.
1. , ?
:
1) ,
. , -
.
2) ,
, ,
.
3) , ,
. ( .)
4) ,
.
,
,
.
.
():
- .
- .
- , , - .
5

[D1]:
.
,




.

2. . .
:
:
-

1) .
2) , .
3) .
4) ,
.
5) , .

, .
, ,
.
3. , .
:
,
, .
1) 1, 2:
-

,
, ,
.

2) 2, ,
. , 2 :
-


,
.
2. .
6

, .
.
. , ,
.
- (S-).
- ,
.
:

. S-:
( . )
.
,
,
S-:
( . ( . ))
.
()
3. .
.
.
.

:
()

.

( )
,

:
((1-1 1-2) 2 (3-1 3-2 3-3) 4)
NIL.
.
7

- , ,
, , .
4. , .

.
. ,
, - .
:
(fn arg1 arg2 ... argN)
( )
(+ 1 2 3 4 5 )
(fn (gn arg1) arg2 ... argN)
( ( ) ( _))
(+ 5 (* 2 3.14 R) X)
, ,
, , .
,
QUOTE, . ,
, .
.
"'".
:
( ( ) )
(fn 'arg1 arg2 ... argN)
(cons '(+ 1 2 3 4 5) arg2 ... argN)
.
( .)
,
.
, -
, ,
.
c .
, ,
, , .
("
- !"),
, .

.
8

2.
-


1. .
(, )
.

.
:
1)

. .
- S-.
,

.
2)

-
S- ,
. S. , ,
S-,
,
.
3)

,
/ , S.

.
C
.
.
, .

Nil
ATOM
LISP
2
_

4139131
.
,
.
,
.

. ,
.
,
.
CONS ( consolidation),
CAR CDR
(content of adress part of register , content of decrement part of
register).

Cons

X)

X
Car

X)

Cdr

X)

Nil . ,
ATOM Nil,
(ATOM ) :

NIL

10

ATOM , Nil , - ,
. , -

.
:
(C )

(B C )

(C (A B))

S- (list-notation).
,
, .
.
: :
((A B) C)
11

((A B) (D C))
((A B)(D(C E)))

CONS
CAR-CDR.
CONS - , ,
, .
, ,
, - .
CAR , -
.
CDR , .
, .. .
ATOM - , .
, .
EQ , .
2.1

.

CONS
CONS
CONS
CONS
CONS
CAR
CAR
CAR
CAR
CDR
CDR
CDR
CDR
CDR

A Nil
(A B) Nil
A (B)
( CONS) ( C )
A (B C)
:

(A B C)
(A (B C))
((A B) C)
A

(A )
(A B C D)
(A (B C))
((A B) C)
A

(A )
((A B) )
(A B)
((A B) C)
(A B C)
A
A
(A B)

Nil
(B C D)
((B C))
(C)

12

CDR
CAR
CAR
CAR
CONS
CAR

(A B C)
CDR
(A C)
CAR
A (B)
CONS

CONS
CDR

A (B)
CONS

(B C)
B
A

(A
B)
A
(A
B)
(B)

ATOM
ATOM

VeryLongStringOfLetters
(A B)

CDR
ATOM
ATOM
ATOM

(A B)
CDR
Nil
()

T
Nil -

(B)
Nil
T
T

EQ
EQ
EQ
EQ
EQ

A A
A B
A (A B)
(A B)
(A B)
Nil ()

T
Nil
Nil

T


,
. ,
Nil.
,
T (true) ,
, , .



S-
13

(dot-ntation),
.
, ATOM1 ATOM2,

ATOM1

ATOM2

S-

( ATOM1 . ATOM2 )

ATOM1, ATOM2
, ,
S-.
S- - S, .
, .
.
:
(A . B)

(C . (A . B))

S- CONS
CAR-CDR.
. S-:
14

((A . B) . C)
((A . B) . (D . C))
((A . B) . (D . (C . E)))
, CONS,
,
CAR CDR, :
CONS - , ,
.
, - .
CAR , ,
, .. .
CDR - , ,
, .. .
S-

CONS
CONS
CONS
CONS

CAR
CAR
CDR
CDR

A B
(A . B) C
A B
(
CONS) C

(A . B)
((A . B) . C)

(A . B)
(A . (B . C))

(A . B)
((A . B) . C)
(A . B)
((A . B) . C)

A
(A . B)
B
(B . C)

CDR
CAR
CDR
CAR
CONS

(A . (B . C))

CDR
(A . C)

CDR
A B

(B . C)
B
C

(A . B)
15

CAR
CONS
CDR


CONS
A B

CONS

A
(A . B)
B

: ( )

CONS
CAR


x y

CONS

CONS
CDR


x y

CONS

CAR
CDR
CONS

x .
x.

CAR CDR

x
(

CONS )

y
(

CONS )
x

ATOM

CDR
ATOM

(A.B)

Nil

(A.B)

CDR

B
T

EQ

(A . B)

(A . B)

16


.
S-,
Nil, .
Nil, (),
. (A) S- (A
. Nil). (A1 A2 Ak) S- :
(A1 . (A2 . ( . (Ak . Nil) ))).

2.3. S-

List-notation

(A B C )
((A B) C )
(A B (C E))
(A)
((A))
(A (B . C))
(())
(A B . C)

Dot-notation -

(A . (B . (C . Nil)))
((A . (B . Nil)) . (C . Nil))
(A . (B . ((C . (E . Nil)). Nil)))
(A . Nil)
((A . Nil) . Nil
(A . ((B . C) . Nil))
(Nil . Nil)
(A . (B . C))


CARCDR. a d,
CAR CDR ,
c r. CAR-CDR
, .. .
. .
CAR-CDR

Caar


,
:
A

((A) B C)

Cadr
Caddr

(A B C)
(A B C)

Cadadr

(A (B C) D)

B - CDR CAR
C - ( CDR)
CAR
C - (CDR
17

CAR)

:
-

,
, .

- S- - S, . S-.
- S- CONS
CAR-CDR.
-

S- :
, .

CAR, CDR, CONS, EQ,, ATOM


.

3. -
.
-

, , ,
, , .
S-.
1) - .
.
:
18

X
n
Variable1
2
LongSong

2) , ,
, .
:
CONS
CAR
CDR
ATOM
EQ

3)

( ). . ,
, - ,
-.
:

( 1 2 ... )

4) .
:

(1 (2 21 22 ... ) 2 ... )

,
, CAR, CDR, CONS,
ATOM, EQ S-:
19

(CAR (CONS x y)) = x


(CDR (CONS x y)) = y
(ATOM (CONS x y)) = Nil
(CONS (CAR x) (CDR x)) = x
(EQ x x) = T
(EQ x y) = Nil

x.
x
x y


,
.

,
.


5) QUOTE
:
: .
(QUOTE (C O N S T ))
.
:
(C O N S T )


, ,
. ,
,
(quotation), ,
.
QUOTE, .
:
(QUOTE A)

A .

(QUOTE (A B C) )

(A B C) .

(ATOM (QUOTE A)) = T



20

(ATOM (QUOTE (A B C) )) = Nil


(ATOM A)

( (QUOTE (A B C)))


(A B C)
, ..

A,



.

,

. ,
.
6) Lambda-:
:
(LAMBDA (x)

(CAR (CDR (CDR x))) )

|
|_________________|_____
|__________________________


- :
((LAMBDA (x) (atom x)) 123) ; = T

X 123 ,
, .

Lambda,
.
7)
DEFUN,
- , .
DEFUN ,
.
.

(DEFUN (x) (CAR (CDR (CDR x))) ))


21

|
|

| |__________________|_____
|_____________________________

|___________________________________________

Caddr 2.4.
.
,
.
.
,
.
,
, .
,
, .

.
,

.
8) ( ) ,
.
.

COND (condition). ,
.
. :
,
.
,
.

(COND (p1 e1) (p2 e2) ... (pk ek) )


|______|________|__________
|______|____ ___|__________
pi ei : ,
, , , .

22

(if Predicate Then Else) ( Predicate Then


Else) COND
:

(COND (Predicate Then)(T Else))

:
(COND (Predicate Then)
(T

Else )


.
:
(COND ((EQ (CAR x) (QUOTE A)) (CONS (QUOTE B) (CDR x))) (T x) )

T .
x B ,
(CAR x) A.
QUOTE, COND LAMBDA DEFUN
CAR, CDR, CONS, ATOM, EQ
. ,

.
.
,
S-.
:
9) .

. .
23

, :

( x) x

Atom (x)
:= x
:= (Car (x))

(DEFUN (x)(COND ((ATOM x) x)


(T ( (CAR x))) ))

x , ,
x,
(CAR x). x
,
T.
.
. , S-
CAR, S - ,
, ,
. ,
.
,

.
:
((DEFUN (LAMBDA (x)(COND ((ATOM x) x)
(T ( (CAR x))) )))
(QUOTE ((A . B) . C) )
24

DEFUN ,

x ((A . B) . C)).

x = ((A . B) . C))

. .

( (QUOTE ((A (COND ((ATOM x) x)


. B) . C)))

(T ( (CAR
x))) )

(QUOTE ((A . B) . C))

(QUOTE ((A . B) . C))

X = ((A . B) . C)

(COND ((ATOM x) x)
: (ATOM x)
(T
(
(CAR x))) )
(ATOM x)

Nil = ,

.. X .

T = .

( (CAR x))
(CAR x)

(COND ((ATOM x) x)

(CAR x)

X = (A . B)

: (ATOM x)
25

(T
(
(CAR x))) )
(ATOM x)

( (CAR x))

Nil = ,
.. X .

T = .

(CAR x)

(CAR x)

X = A

: (ATOM x)
(COND ((ATOM x) x)
(T
(
(CAR x))) )
(ATOM x)

T .. X

A
X


, ,
.

. .

- .
.
1. .
( )
( x) x

(x < 0)
:= - x
:= x

26

(-)
(DEFUN (LAMBDA (x)(COND ((< x 0 ) (- x))
(T x)

)))

2. .
( )
( N) N

(N = 0)
:= 1
:= N * (N - 1)

(-)
(DEFUN (LAMBDA (N)(COND ((= N 0 ) 1 )
(T ( * N ( (- N 1 ))) ) )))

.

. ,
, :
(+ 1 2 3 4) = 10

,
, .
3.
, .
( )
( x, y) x

(x < y)
:= ( y, x)
(y, x) = 0
:= x
27

:= ( (y, x), x)

[x, y] - , x y.
(-)
(DEFUN (LAMBDA (x y)(COND ((< x y) ( y x))
((= ( y x ) 0 ) x )
(T ( ( y x) x )) ))
)

S-,
- .
S- CAR,
CDR, CONS, ATOM, EQ ,

QUOTE, COND, LAMBDA, DEFUN.
EVAL,
, , -
.

, S-:

,

,

, ..
,
-
,
, ,

.

28

:
- , ,
, , , .
S-.
-

Lambda,
.

S-
CAR, CDR, CONS, ATOM, EQ ,

QUOTE, COND, LAMBDA.

, S-.
QUOTE.


DEFUN.

29

4.

.
-

.

, ( -).
:

.
.
.

.

S.
<> ::= <> <_>
<_> ::= <>
| <> <_>
| <> <_>

- . .
<S-> ::= <>
| (<S-> . <S->)
| (<S-> ... )

S- - , S-,
S-.
/ ,
, ./

30

; - .
.. () S-.
Nil.
- ,
.
:
() = Nil
(a . Nil) = (a)
--(a1 . ( ... (aK . Nil) ... )) = (a1 ... aK)

.
,

.

(, , ..).
, ,
.
.
(), .. ,
. ,
, ,
.

,
, .
, ,
, , , .

<> ::= <>


- ,
- . ,
, .
31

<> ::= <>


| <>
| (<> <> ... >)
| (COND (<> <>) (<> <>) ... )
<> ::= (QOUTE <S->)
| '<S->
<> ::= <>
- , ,
. ,
.
- , .
, QUOTE, COND
.
<> ::= <>
, ,
. ,
, .

QUOTE, .
QOUTE ,
. (') -

.
.
, S, .
- , ,
, .
, ,
.
. ,
.
.
.

.

32

S-.
,
, Nil,
"". , .
S-,
.
( -),
.

. .
<> ::= <>
| (LAMBDA <_> <>)
| (DEFUN <> <>)
<_> ::= (<> ... )
<> = <>
- , ,
.
, - , ,
LAMBDA DEFUN.
.
. , CAR, CDR ..
-,
,
( ).
, , - .
. ,
DEFUN.

EVAL, ,
S-.
.
S-.
AMONG S-.
33

(DEFUN among (x y) (COND


((ATOM y) (EQ x y))
((among x (CAR y)) (QUOTE T))
((QUOTE T) (among x (CDR y) ))
)
)
EQUAL - , S-.
"" "" . (
EQ .) EQUAL
(
)
(DEFUN equal (x y) (COND
((ATOM x) (COND
((ATOM y) (EQ x y))
((QUOTE T) (QUOTE NIL))
)
)
((equal (CAR x)(CAR y)) (equal (CDR x)(CDR y)))
((QUOTE T) (QUOTE NIL))
)
)
-:
(DEFUN _ (x y) (equal x y))
SUBST - x, y, z,
x y S- z.

S-

(DEFUN subst (x y z) (COND


((equal y z) x)
((ATOM z) z)
((QUOTE T)(CONS
(subst x y (CAR z))
(subst x y (CDR z))
)
)
)
)
34

(subst '(x . A) 'B '((A . B) . C))

; = ((A . (x . A)) . C)

(subst 'x '(B C D) '((A B C D)(E B C D)(F B C D))) ;= ((A . x)(E . x)(F . x))
; - .
equal
. ,
:
(DEFUN (x y z) (subst x y z))
( '(x . A) 'B '((A . B) . C))

;= ((A . (x . A)) . C)

NULL - , S-.
, , .
"" , - Nil.
(DEFUN null (x) (COND
((EQ x (QUOTE Nil)) (QUOTE T))
((QUOTE T) (QUOTE Nil))
)
)

,
.
(DEFUN pair_to_list (x) (CONS (CAR x) (CONS (CDR x) Nil)) )

(DEFUN list_to_pair (x) (CONS (CAR x) (CADR x)) )


,
CONS, .. .


, .
35

APPEND - x y, .
(DEFUN append (x y) (COND
((null x) y)
((QUOTE T) (CONS
(CAR x)
(append (CDR x) y)
)
)
)
)
(append '(A B) '(C D E))

;= (A B C D E)

MEMBER - x y, S x y.
(DEFUN member (x y) (COND
((null x) (QUOTE Nil))
((equal x (CAR y)) (QUOTE T))
((QUOTE T) (member x (CDR y))
)
)
PAIRLIS - x, y, al,
x y - al.
,
,
.

.
(DEFUN pairlis (x y al) (COND
((null x) al)
((QUOTE T) (CONS (CONS (CAR x)
(CAR Y) )
(pairlis (CDR x)
(CDR y)
al)
) )
)
)
(pairlis '(A B C) '(u t v) '((D . y)(E . y)))

;= ((A . u)(B . t)(C . v)(D . y)(E . y))


36

ASSOC - x al. al - ,
, pairlis, assoc
, x. ,
, .
(DEFUN assoc (x al) (COND
((equal x (CAAR al)) (CAR al))
((QUOTE T) (assoc x (CDR al))
) )
)
(assoc 'B '((A . (m n)) (B . (CAR x)) (C . w) (B . (QUOTE T))))

;= (B . (CAR x))

- .
SUBLIS - al y, ,
AL ((u1 . v1) ... (uK . vK)),
u , Y - S-. sublis
Y, Ui,
Vi, .
S- Y U
V AL.
SUB2, S-, -
SUBLIS:
(DEFUN sub2 (al z) (COND
((null al) z)
((equal (CAAR al) z) (CDAR al))
((QUOTE T) (sub2 (CDR al) z))
))
(DEFUN sublis (al y) (COND
((ATOM y) (sub2 al y))
((QUOTE T)(CONS
(sublis al (CAR y))
(sublis al (CDR y))
) )))
.
(sublis '((x . )(y . ( ))) '(x y))

37

;= ( ( ))

INSERT z x al.
(DEFUN insert (al x z) (COND
((null al) Nil)
((equal (CAR al) x) (CONS z al))
((QUOTE T) (CONS (CAR al) (insert (CDR al) x z))
)
)
(insert (a b c) b s)

; = (a s b c)

ASSIGN ,
.
. ,
-,
.
(DEFUN assign (x v al) (COND
((Null al) (CONS (CONS x v) Nil ))
((equal x (CAAR al))(CONS (CONS x v) (CDR al)))
((QUOTE T) (CONS (CAR al) (assign x v (CDR al))
)
)
(assign a 111 ((a . 1)(b . 2)(a . 3)))
(assign a 111 ((c . 1)(b . 2)(a . 3)))
(assign a 111 ((c . 1)(d . 3)))

;= ((a . 111)(b . 2)(a . 3))


;= ((c . 1)(b . 2)(a . 111))
;= ((c . 1)(d . 3) (a . 111))

: , __,
__, _, , , , ,
_, , ,

.
: REVERSE
:
(defun reverse (m) (cond ((null m) NIL)(T (append(reverse(cdr m))
(list(car m)) )) ))
38


:.
(defun rev (m n) (cond ((null m) N)(T (rev(cdr m) (cons (car m) n)))))
(defun reverse (m) (rev m Nil))

:
-

.
, .

- , .
, QUOTE, COND
.

,
, .

39

5.

,
.
-

.
.

EVAL-APPLY.
.

- ,
, ,
,
, . (, ,
, ,
.)
eval expr - ,
.

, , ,
.
:
- .
. , x,
elem, , .
- , QUOTE,
. , (QUOTE T)
T, "".
-
. ,

(COND ((ATOM x) x)
((QUOTE T) (first (CAR x) )) )


.
"".
40

-
.
.
. ,
(first (CAR x)) CAR
x,
first.
- ,
, CAR, CDR, CONS ..,
, , , first.
,
- ,
.
- -, , ,
-
. , -,
(LAMBDA (x) (COND ((ATOM x) x)
((QUOTE T) (first (CAR x) )) ))

,
x.
first, .
-

DEFUN,
,
. ,
LAMBDA- ,
DEFUN,
fisrt, .

(DEFUN first (x) (COND ((ATOM x) x)


((QUOTE T) (first (CAR x) )) ))

, DEFUN ,
.
,
:
- (cons, car, cdr, atom, eq),
41

- (lambda, defun),
- ( ),
- (, quote, cond, eval).

eval, ,
:
,
, eval.
(eval '(fn arg1 ... argK))
"fn" "arg1, ..., argK".


-.
(eval '((LAMBDA (x y) (CONS (CAR x) y)) '(A B) (C D) ))
= (A C D)
eval apply
.
-
. .
.

42

. .
<> ::= <>
| (QOUTE <S->)
| (COND (<> <>) ... (<> <>))
| (<> <> ... <>)
<> ::= <>
<> ::= <>
<> ::= <>
| (LAMBDA <_> <>)
| (DEFUN <> <>)
<_> ::= (<> ... )
<> = <>
<> ::= <>
:
(DEFUN eval0 (e) (eval e '((Nil . Nil))))

eval0 , eval
,

.
(defun eval(e a) (COND
( (atom e) (cdr(assoc e a)) )
( (eq (car e) 'QUOTE) (cadr e))
((eq(car e) 'COND) (evcon(cdr e) a))
( T (apply (car e) (evlis(cdr e) a) a) )) )
(defun apply (fn x a) (COND
((atom fn)(cond
((eq fn 'CAR)(caar x))
((eq fn 'CDR)(cdar x))
((eq fn 'CONS) (cons (car x)(cadr x)) )
((eq fn 'ATOM)(atom (car x)) )
((eq fn 'EQ) (eq (car x)(cadr x)) )
((QUOTE T) (apply (eval fn a) x a)) ) )
)
((eq(car fn)'LAMBDA) (eval (caddr fn) (pairlis (cadr fn) x a) ))
((eq (car fn) 'DEFUN) (apply (caddr fn) x (cons (cons (cadr fn)(caddr fn)) a)))))

43

: , -
, .
assoc pairlis .
(DEFUN evcon (c a) (COND
((eval (caar c) a) (eval (cadar c) a) )
(T
(evcon (cdr c) a)

) ))

*) . , .. C.
(DEFUN evlis (m a) (COND
((null m) Nil )
(T
(cons(eval (car m) a)
(evlis(cdr m) a)

)) )

(DEFUN eval0 (e) (eval e ObList ))

ObList,
. ObList
Nil,
T.
.
eval - . - ,
,
.
CAR - QUOTE, ,
CADR .
CAR - COND, - .
EVCON, ( ),
( )
, ,
"". EVAL .

.

44

EVLIS ,

APPLY.
apply - . - ,
: (car cdr cons
atom eq).
. , -
, .
LAMBDA,
, ( ) eval .
DEFUN,
,
.
apply,
-
. ""
, ,
. ,
- DEFUN, ,
.
,
,
.
,
. ,
.
1)
, . .
( Clisp-),
, ,
.
2) CAR CDR
. ,
, .
,
, .
-
, , ,
.
EQ ,

45

,
.
3)
, , ,
. COND
. -
(QUOTE T) (QUOTE NIL).
T Nil .
4)
,
, .
, ,
, ,
.
.
.

,
. - T NIL. -
. .

.
.
T NIL.
, ,
:
. S-, NIL,
. EQ
:
1) , EQ NIL.
2)

, - .

3) , , T NIL
, .
4) EQ T NIL. ,
.

, .
:
46

1) , S-.
2) ,
S-.
3) S.
4) ( S-),
, S-,
.
5) ,
S-, ,
.
,
.
:
-

,
.


, ,
, .

47

6.
:
- ,
- ,
- ,
- ().
,
. , , , , ,
, ,
, ,
, .
- . ,
, , -
.
- .
. , , . ,
-
. , . -,
,
. ,
. ,
,
. -, ,
, .
,
.
, , ,
- , - .
:
- ;
- , ;
- .

. ,
,
,
. , ,
:
- ;
1

- ;
- .

,
.

,
.
"". ,
.
, ,
, .. .
, ,
, -
. , ,
,
.
, (
) , .
,
. ,
- , ,
.
, ,
, .

.
. - ,
:

.
, ,
. ,
, ,
, .
,
:
1
-123
9876543210000000000000123456789

8/12
3.1415926

;= 2/3

:
" , , "

- , ,
, .
:

(+ 1 2 3 4 5 6)
(- 12 6 3)
(/ 3 5)

;= 21
;= 3
;= 3/5


.
(string-equal " 1" " 1")
(ATOM "a+b-c")
(char "1" 4 )

;= Nil
;= T
;= "1"

,
.
, "'" .
"QUOTE".
,

1
"abc"

;= 1
;= "abc"

.
COND, Nil .
.

(cond ((= 1 2) 1)
( 1
2)
3

;=2

; - .

,
, , .
(, - .)
- ,
.
, ,
.


.
. - ,
:
- , ,
, :
, "'" .
"QUOTE" :
:
COND:

,
, . (, - .)
- , .
,
, .

-
,
.
1.
.

(defun next (xl)


; :
(cond
;
(xl (cons (1+ (car xl))
; 1
(next (cdr xl))
; ,
)
) )
)
;
4

(next '(1 2 5 ))

; = (2 3 6 )

2. ""

(defun 1st (xl)


(cond
(xl (cons (caar xl)
(1st (cdr xl))
)
) )
)

; "" = CAR
;
; CAR
; ,
;

(1st '(( )(one two )(1 2 )) )

; = ( one 1)

3.

(defun lens (xl)


;
(cond
(xl (cons (length (car xl))
(lens (cdr xl))
)
) )
)


;
;
; ,
;

(lens '((1 2 ) () (a b c d ) (1 (a b c d ) 3 )) )

; = (2 0 4 3 )

,
map-el, "car", "1+"
"lenth" fn:

(defun map-el (fn xl) ; XL


; FN
(cond
; XL
(xl (cons (fn (car xl) )
; FN XL1
(map-el fn (cdr xl))
)

; ,
;

: Clisp ,
funcall:
5

next, 1st lens :

(map-el #'1+ xl)


(map-el #'car xl)

; :
; "" = CAR

# x ;= (FUNCTION x) - -
(map-el #'length xl)

(map-el #'1+
'(1 2 5 ))
; = (2 3 6 )
(map-el #'car '(( )(one two )(1 2 )) ) ; = ( one 1)
(map-el #'length '((1 2 ) () (a b c d ) (1 (a b c d ) 3 )) )
; = (2 0 4 3 )

.

.
:
(defun next
(defun 1st
=
(defun lens

(xl) (map-el #'1+ xl ))


; :
(xl) (map-el #'car xl ))
; ""
CAR
(xl) (map-el #'length xl )) ;

.
4. sqw,
(defun sqw (x) (* x x))
(sqw 3)

;
; = 9

, sqw:

(defun map-el (fn xl)


(cond
(xl (cons (funcall fn (car xl) )
;
;
(map-el fn (cdr xl) )
) ) ) )1

(defun sqware (xl) ;


;
(cond
; ,
(xl (cons (sqw (car xl))
; sqw
(sqware (cdr xl)) ; ,
)
) )
)
;
(sqware '(1 2 5 7 ))

; = (1 4 25 49 )

map-el:
(defun sqware (xl) (map-el #'sqw xl))

sqware- ,
.
(CAR xl), .. :
(defun sqware- (xl)
(cond
(xl (cons (* (car xl) (car xl) )
;
;
(sqware- (cdr xl))
)
) )
)

5. tuple,
:
(defun
(tuple
(tuple
(tuple

tuple (x) (cons x x))


3)
; = (3 . 3)
'a)
; = (a . a)
'())
; = (() . ()) = (() ) - !

, :
(defun duble (xl) (map-el #'tuple xl))
;
(duble '(1 (a) ()))
; = ((1 . 1) ((a) a) (()))

.
6. , ..
, :
(defun pairl (al vl)
;
(cond
; AL ,
(al (cons (cons (car al) (car vl))
; .
(pairl (cdr al) (cdr vl)) ; VL ,
7

; CDR NIL
)

(pair '( two three) '(1 2 ))


; = (( . 1)( . 2)(two )(three ))

7.
fn:
(defun map-comp (fn al vl)

; fn
; AL VL

(cond
(xl (cons (fn (car al) (car vl))
; FN
(map-comp (cdr al) (cdr vl))
) )
)

,
, . ,
, , :
(map-comp #'+ '(1 2 3) '(4 6 9))
; = (5 8 12 )
(map-comp #'* '(1 2 3) '(4 6 9))
; = (4 12 27 )
(map-comp #'cons '(1 2 3) '(4 6 9))
; = ((1 . 4)(2 . 6)(3 . 9))
(map-comp #'eq '(4 2 3 ) '(4 6 9))
; = (T NIL NIL )

, ,
map-comp.
8. , - ,
, .
(defun mapf (fl el)
(cond
; ,
(fl (cons ((car fl) el )
;
;
(mapf (cdr fl) el)
; ,
)
) )
)
;
(mapf '(length car cdr) '(a b c d ))

; = (4 a (b c d ))


.
, , .


4 5 :
9 duble sqwure
.
9 ,

, .
9 , -
.
,
.
, , -
.
,
.

lambda .
expr (x1 ... xK)
lambda-:
( lambda (x1 ... xK) expr )
, .
Defun , , , .
9. duble sqwure 4 5
:

(defun sqware (xl) (map-el


(defun duble (xl) (map-el

(lambda (x) (* x x)) xl))


(lambda (x) (cons x x)) xl))

,
.
, ,
,
,
.

,
, .
10. :

(defun decart (x y)
(map-el #'(lambda (i)
)

(map-el #'(lambda (j) (list i j))


y)
x) )


(decart '(a s d) '( e r t))

(((A E) (A R) (A T)) ((S E) (S R) (S T)) ((D E) (D R) (D T)))


((A E) (A R) (A T) (S E) (S R) (S T) (D E) (D R) (D T))

, map-el, map-comp ( 7),


cons ,
.
, .
, , cons
append.
11. . .
(defun list-ap (ll)
(cond
(ll (append (car ll)
(list-ap (cdr ll) )
)
) )
)
(list-ap '((1 2)(3 (4))))

; = (1 2 3 (4))

map-ap:
10

(defun map-ap (fn ll)


(cond
(ll (append (fn (car ll) )
(map-ap fn (cdr ll) )
)
) )
)
(map-ap 'cdr

'((1 2 3 4) (2 4 6 8) ( 3 6 9 12)))
; = (2 3 4
4 6 8
6 9 12)

, :
(defun decart (x y)
(map-ap #'(lambda (i)

(map-el #'(lambda (j) (list i j))


y)
)
x)
)
(decart '(a s d) '( e r t))
; = ((A E) (A R) (A T) (S E) (S R) (S T) (D E) (D R) (D T))

append
: .
,
. ,
, .
12. :

(defun heads (xl) (map-ap


#'(lambda (x)(cond (x (cons (car x) NIL))))
; ,
;
xl
)
)
(heads '((1 2) () (3 4) () (5 6)) )
; = (1 3 5)

. ,
, ,
, , ..
.
,
.
13. .

(defun sum-el ( xl)


(cond ((null xl) 0)
(xl (+ (car xl)
11

(sum-el (cdr xl) )


)

(sum-el '(1 2 3 4 ) )

; = 10

, "+"
:
(defun red-el (fn xl)
(cond ((null xl) 0)
(xl (fn (car xl)
(red-el fn (cdr xl) )
(red-el '+ '(1 2 3 4 ) )

))))
; = 10

- map-ap -
.
,
, .
:
- .
- .
- .
- .
- ,
.
- .
- Clisp
.
- ,
.

12

7.
-

. -.

.
.

,
. -
. ,
-,
.
: .
, .
(UNION) (INTERSECTION) .
:
UNION INTERSECTION ,
. ,
, (MEMBER).
:
MEMBER , - ,
. , .
:
:
- ,
Nil, .. .
- ,
T, .. .
- , ..
.
member ( a, x) a, x

(a)
:= Nil
(a, (x) )
:= T
:= member (a, (x))

13

UNION , X Y - ,
. ,
Y.
:
:
- ,
, .. .
- ,
, ..
, .
-
.
UNION ( x,y) x, y

(x)
:= y
member ( (x), y )
:= UNION ( (x), y)
:= cons ( (x), UNION ( (x), y))

INTERSECTION , X Y - ,
. ,
, Y.
:
:
- ,
- .
- ,

.
-
.
INTERSECTION ( x,y) x, y

(x)
:= Nil
member ( (x), y )
:= cons ( (x), INTERSECTION ( (x),
y))
:= INTERSECTION ( (x), y)

14

, -
DEFUN. :

(DEFUN MEMBER (A X)
;
(COND
((NULL X)
Nil)
((EQ A (CAR X)) T)
(T
(MEMBER A (CDR X)) )
)
)
(DEFUN UNION (X Y)
;
(COND
((NULL X) Y)
((MEMBER (CAR X) Y) (UNION (CDR X) Y) )
(T (CONS (CAR X) (UNION (CDR X) Y))) )) )
))
(DEFUN INTERSECTION (X Y)
;
(COND
((NULL X) NIL)
((MEMBER (CAR X) Y) (CONS (CAR X) (INTERSECTION (CDR
X) Y)) )
(T (INTERSECTION (CDR X) Y))
))
(INTERSECTION '(A1 A2 A3) '(Al A3 A5))
;
(UNION '(X Y Z) '(U V W X))
;
- .
- DEFUN. - (A1
A3). - (Y Z C B D X). ,
, , .
- - , ,
- . DEFUN
. -
, - MEMBER, UNION, INTERSECTION.
, - DEFUN
,
15

, ,
.

, 1.5
, ,
.
1) . ,
.
. EVAL,
APPLY .
2) . .
, , .
3) ,
.
4) (QUOTE T), (QUOTE NIL). T, NIL,
EVAL.
5) , .
6) .
, , (
).
7) ,
.
:
((A . B) X (C . (E F D))) - S-.

((A . B) . ( X . ((C . (E . ( F . (D . Nil))) ) . Nil)))

((A . B) X (C E F D))
16

8) (A B C . D) (A . ( B . ( C . D) )).
, , (A. B C) (A B . C
D) . (
. . D , Nil, ,
D)
9) .
.
.
S- - PRINT,
- READ. LOAD.
:
(LOAD 'TEST.LSP)
,
PRINT.
:
(PRINT (INTERSECTION '(A1 A2 A3) '(Al A3 A5)) )
(PRINT (UNION '(X Y Z) '(U V W X)) )
(PRINT (UNION (READ) '(1 2 3 4)) )
; '(1 2 3 4)

- , .
, .
, ,
.
, ,
APPLY. APPLY , LAMBDA,
.

17

, , EVAL.
EVAL .
. ,
.
. ,
S-:

((LAMBDA (X Y) (CONS X Y)) 'A 'B)


:
(LAMBDA (X Y) (CONS X Y))
: (A B)
EVAL APPLY. (. 6).
(apply #(LAMBDA (X Y) (CONS X Y)) (A B) Nil )
APPLY - EVAL
.

(eval (CONS X Y) ((X . A) (Y B) Nil))


EVAL CONS,
.

(Cons A B) = (A . B)
,
. Let
.
.
:

18

(defun UNION- (x y) (let ( (a-x (CAR x))


(d-x (CDR x)) )
(COND ((NULL x)
y)
((MEMBER a-x y) (UNION d-x y) )
(T
(CONS a-x (UNION d-x y)) ) )
))

- ,

(-). ,
-,
. ,
(a . v) a-.
, .
(property list - -), -
, , a-.
, , ,
, , ,
. EVAL . -.
,
SET.
, Nil T,
. T, Nil (,
) .
.
, , ,
. , .
.
, .
.
-
, . -
DEFUN, . APPLY ,
, - .
, - , ,
, , -
. , ,
.
19

S-

.
, .
,
:
1) -.
2) .
3) S-,
. ,
.
EVAL
APPLY. , EVAL (CONS X Y), X Y,
CONS . EVAL (QOUTE X), X
. QUOTE - ,
.
.
, . COND,
, EVCON.
,
.
.

.
: -
.
,
.
FUNCTION ,
FUNCALL

.
(funcall f a1 a2 ... ) = (apply f (list a1 a2 ...))
, ,
.
-.
- .
20

1.
(CAR '(A B)) = (CAR (QUOTE(A B)))
: CAR
: ((A B))
A. , .
CAR (A B). .. APPLY
.
(LAMBDA(X)(CAR X)) CAR. ,
.
2.
(CONS 'A '(B . C))
: CONS
: (A (B . C))
(A . (B . C)) (A B . C)
3.
(CONS '(CAR (QUOTE (A . B))) '(CDR (QUOTE (C . D))) )
: CONS
:
((CAR (QUOTE (A . B))) (CDR (QUOTE (C . D))))

((CAR (QUOTE (A . B))) . (CDR (QUOTE (C . D))))
21

, . (CAR (QUOTE
(A . B)) A (A . D) CONS.
:
(CONS (CAR (QUOTE (A . B))) (CDR (QUOTE (C . D))) )
.
, CAR CDR LAMBDA
. - CONS
EVAL -. -
,
((LAMBDA (X Y) (CONS (CAR X) (CDR Y))) '(A . B) '(C . D))
:
(LAMBDA (X Y) (CONS (CAR X) (CDR Y)))
:
((A . B)(C . D))

(EVAL '(CONS (CAR (QUOTE (A . B))) (CDR (QUOTE (C . D))))


Nil)
: EVAL
:
((CONS (CAR (QUOTE (A . B))) (CDR (QUOTE (C . D)))) Nil)
(A . D)

22

((LAMBDA (X Y) (CONS (EVAL X) (EVAL Y))) '(CAR (QUOTE (A . B)))


'(CDR (QUOTE (C . D))) )
:
(LAMBDA (X Y) (CONS (EVAL X) (EVAL Y)))

:
((CAR (QUOTE (A . B))) (CDR (QUOTE (C . D))))
,
-
, .
:
-

-
-.

.
.

,
,
.

.
.

-
.

23

8.

:

(lazy evaluation)
,
,
. ,
,
" ", . QUOTE COND,
, . ,
,
.

, ,
.
(lazy evaluation).
,
.
.
" ".

(x1 x2 x3 ... )
x1
(x1 ( ))

. - ,
, ..
.

24

: M N .
:
(defun _ (M N) (cond ((> M N) Nil)
(T(cons M (_ (1+ M) N)))))
(defun (X) (cond ((= X 0) 0)
(T (+ (car X)( (cdr X))))) )
|| - @ -
.
, ,
.
,
.
, ,
. , ,
.

, CONS
:
(defun _ (M N) (cond ((> M N) Nil)
(T(cons M ( || (_ (1+ M) N))))))
(defun (X) (cond ((= X 0) 0)
(T (+ (car X)( @ ( (cdr X))))) ))
.
, T -
, Nil - .
(Nil e AL ) ( T X ), X = (eval e AL )
e, ,
.

. , , M
:
(defun (M) (cons M ( || ( (1+ M) ))))

25

,
, K :
(defun (K Int) (cond ((= Int Nil) Nil)
((= K 0) Nil)
(T (cons (car Int)( ( @ (cdr Int))) )) ))

|| @:
||e = > (lambda () e )
@e = > (e ),

|| - EVAL @ .
, , .

.
, , ,

, ,
.

.
,
, .

,
( 0, ..)
-
.
:
-

.

.
26

27

9. ()
-

Prog
(-)

. ,
, .

.
.
,
.
, .
-,
. ,
, - prog-,
. ,
,
.
, prog-
Length.
. Length -
. :
" L.
z v.
0 v.
L z.
A: z NIL,
,
v.
z cdr , z.
v , v.
A"

28

-
.
LENGTH, , :

function LENGTH (L: list) : integer;


var Z: list;
V: integer;
begin
V := 0;
Z := l;
A: if null (Z) then LENGTH := V;
Z := cdr (Z);
V := V+1;
goto A;
end;
-, :
(defun
LENGTH (lambda (L)
(prog (Z V)
(setq V 0)
(setq Z L)
A
(cond ((null Z)(return V)))
(setq Z (cdr Z))
(setq V (+ 1 V))
(go A) ))) ))
;;====================================
(LENGTH '(A B C D))
(LENGTH ((X . Y) A CAR (N B) (X Y Z)))
. 4 5 .
Prog , :
(PROG, , ... )
, ,
. A ,
"COND".
PROG .
NIL ().
29

,
lambda. NIL, ,
- .
SET. pi
3.14 :
(SET (QUOTE PI)3.14)
SETQ SET, .
(SETQ PI 3.14)
. SETQ . SET SETQ
.
SET SETQ .
GO-, (GO A) ,
, A, A
prog.

. ,
, .
RETURN - . return ,
. .
, Return,
NIL.
Prog- .
: REV, ,
Prog-.

(DEFUN rev (x)


(prog (y z)

function rev (x: list) :List


var y, z: list;
begin
A: if null (x) Then rev := y;
z := cdr (x);

A (COND ((null x)(return y)))


(setq z (CDR x))
(COND ((ATOM z)(goto B)))

if atom (z) then goto B;


30

(setq z (rev z))


B

(setq y (CONS z y))


(setq x (CDR x))
(goto A)

))

z := rev (z);
B: y := cons (z, y);
x := cdr (x);
goto A
end;

rev , rev (A ((B C) D)) ((D (C B))A) .


, prog ,
.
- go, set return,
prog. ( Go, Set, Return PROG
COND, PROG.
.)
, , .
, -
cond-, ..
. , Nil.

, .
(T NIL) .
SET SETQ a-
, ,
(. assign 4).
, , .
(DEFUN set (x y) (assign x y Alist))
, ,
: , ..
, .
:
(setq x 'y)
(set x 'NEW)
(print x)
(print y)

31

Y NEW.


,
, .
, . ( ,
.)
. () ,
.
, ,
, .
, .
.
.
, , .
Common Lisp
.
symbol-function symbol-value .
symbol-plist. remprop Clisp
. :
(setf (get ) )
.
, , (, ,
), , .


-.

, .
, ,
,
.
, , .

, : .
- .
.
32

- :
1) , ,
. ,
.
2) ,
. ,
,
.
3) , ,
.
(A (B C)).
A, B C
(cons A (cons (cons B (cons C NIL)) NIL))
, list,
(list A (list B C))
x x = (A B C), A, B C,
,
A = (car x)
B = (cadr x)
C = (caddr x)
grp, (X (Y
Z)) (X Y Z).
(grp x) = (list (car x) (list (cadr x) (caddr x)))
grp X , .

()
33


.
,
.
, .
, - cons,
, . , ,
subst, ,
.
,
. Set Prog ,
CONS.

- rplaca
rplacd.
.
-.
(rplaca x y) x y. - x, x, ,
. rplaca

(rplaca x y) = (cons y (cdr x))


: cons
.
(rplacd x y) x y.
!
.
, ,
equal subst.

, , Clisp-, nconc, mapc ..
grp. ,
,
A

:
34


cons. - , ,
cons , grp rplaca rplacd.
:
A

(cons (cadr x) (cddr x)) .


:
(rplaca (cdr x) (cons (cadr x) (cddr x)))
.
(rplaca (cdr x) NIL).
pgrp :
(pgrp x) = (rplacd (rplaca (cdr x) (cons (cadr x) (cddr )x))) NIL)
pgrp . ,
, ((B C)). , pgrp
, .

. .
:
-


.
, - .
35

,
, , .

.

.

36

10.
. , :
- ,
- ,
- ,
- .
-
:
-

,
,
,
.

, "
" (),

:
1) .
2) , (,
..) ,
.
3) -

( ..), .
4)
, , .
,
, .
1) , ,
, .
.
2)
,
.

37

3) ,
, .

, .
,
,
, ,
, .
- Lisp 1.5
Common Lisp, .
- , .

,
- , , , -, , , ,
, .
, ,
. , GNU Clisp, Python, CMUCL
., , ,
.
, .

. (Pure Lisp, Lisp 1.5, Lisp 2, Interlisp,
CommonLisp, MicroLisp, MuLisp, Sail, Hope, Miranda, Scheem, ML, GNU Clisp, CLOS, Emacs,
Elisp, xLisp, Vlisp, AutoLisp, Haskell, Python, CMUCL). Common Lisp
, . GNU
Clisp, xLisp, CMUCL Common Lisp.

.

, .
,
.

.
.
.
.
.
. -
.

.

38

-
- , ,
(atom, eq, cons, car, cdr,), ,
, (quote, cond,
lambda, eval).
. - , - .
. -
, ,
. - ,
.
- .
.
. XX

, , , ,
, . AutoCAD ,
, Emacs
UNIX/Linux.
.
, , ,
,
.,
.
, ,
.

.
.
, ,
,
.

39

( )
, ,
:
1958 - - .
1962-1964 - - .
1964 - .
( . - ,
,
.)
1972-1974 - , .
1972-1980 - .
1978 -.
1965-1990 -
,

1992-2002 - -, CMUCL.
.
1968 -
, .

40


1. McCarthy J. LISP 1.5 Programming Mannual.- The MIT Press.,
Cambridge, 1963, 106p.
2. . .- .: , 1983
3. ., . ., .1,2, .: , 1994
4. .. . , , . //
2000, N 29.
5. .. . //
. 2002, N 2-4.
6. .., .. .
. // . 2002, N5, . 49-58
7. http://www-formal.stanford.edu/jmc/ - .

8. http://www.marstu.mari.ru/mmlab/home/lisp/title.htm
..
9. http://grimpeur.tamu.edu/~colin/lp/ -

10. http://www.paulgraham.com/onlisptext.html - Clisp


, ANSI Clisp
11. .. . .
. /.: . , 2004. 280 .

41

Term

algorithm

a-list

Association list

,
, ..
.

, .

.
, ((VAR1 . VAR2) (B . (U V (W)))(C . Z))

Atom
Atomic symbol

total
functio
n
Destruc
tive
functio
ns
Lazy
evaluati
on

Lexical
closure

S-.

, . : AI NIL
_2-1

,
Basic functions:
CAR, CDR, CONS,
S- ,
EQ, ATOM
.

Pure
functio
n
Indicator

,
-
,
. , nconc
,

,
.

.
+
.

FUNCTION.

,

,

(),

.

42

Interpreter

,

. ,

.

True
values
compiler

.
T NIL.
,
() .
, LISP
.
,
S-,
, .

composition


.
.
: (CONS (CAR Y) X)

Constant

,
.
: T NIL 1 2 3

context
Lisp
Lisp
process
or

,
60-
.

, SECD
Scheme -
.
70- XX .
, AND, OR NOT,

.

Logical form

Lambda notation

,
() ,

.

Memofunctio
n

Imaging, map
variable
Predicate
Pseudofunctio
n
Empty
list

- ,

.
(true) (false)

, .
NIL
43

Recursion
Garbage collector,
reclaimer

,
.
,
,
, .
,
.

Symbol

,
, , ,
.

Property list, p-list

,

.

Special form

,
.
, ,
.

Free variable

Propert
y

Bound variable

, ,
.

, .


().
,

.
, ,
LAMBDA.
,
,
LAMBDA-. ,
:
(( LAMBDA (X Y ) E )(QUOTE (A B)) (QUOTE C))
Y C, E, X
(A B).

Free-storage list

,
. CONS
,
.
,
.

List

S-,
NIL.
LISTP(x) = NULL(x) OR (NOT (ATOM (x))
AND LISTP (CDR (x)))

List notation

S-
( m1 m2 mn)

(m1 . (m2 . ( . (mn . NIL)))

44

Atom
table
Tag

Total
functio
n
Dot notation


.
.
.

S-
S-, .
Lisp-.
.
: ( . ) ( . NIL)

Pointer

Univers
al
functio
n

Conditional
expression

- , ,
.
,
, .
, S,

.

,
.
: EVAL, APPLY
,
.

,
.
: ( COND (( < A 0) B) (T C))

APPLY

Form

, ,

.

.

Functional argument

, .
#
(FUNCTION).

Function

Pure
lisp

APPLY

.

DEFUN. -
.

: CAR,
CDR, CONS, EQ, ATOM, COND,
LAMBDA,QUOTE.
.
EVAL APPLY -
.

APPLY FUNCALL

45

CAR

CAR

S-.
Contents of Addres of Register.
.

CDR

CDR

S-.
Contents of Decrement of Register

EVAL

EVAL
NIL
S -

NIL
S - expression

. .

.

46