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

,

..

..
-

.

4
5
6.040302
-

2012


13 2012
7

-
22 2012
3

1.
1.1.
2.
2.1.
2.2.
2.3.
2.4. EVAL
3.
3.1. -
4.
4.1.
5.

4
5
6
8
8
15
18
19
20
23
25
26
35
38


, : .
(), . , (), , ,
. () , ,
, ,
.
, ,
, , , ,
.
, C, C++, Pascal, Java
. :
.
, , .
, , Erlang, OCaml,
Haskell, Scheme ( 1986), R (), Mathematica ( ), J K ( ), XSLT (XML). ,
, SQL Lex/Yacc
.
- ,

.
4

1.

LISt Processing. ,
:
;
,
.
, -
.

. ( ) .
, , .


. ,
- , ,
.

,

. , , .
, , , ,
.
.
.
5

1.1. .
1.1. , ,
,
, , .
.
,
, () .
1 ,
,
+

&

<

>


?, ! , , .
, , ,
. , -
,
, , .
T NIL :
T , NIL
2. T NIL

.
T NIL , , .
1

, ANSI.
.
2 NIL .
6

,
, . PI .
, DEFCONSTANT. , ,
, .
1.2.
, .
3.
1.3. ,
(). .
.
(a b (c d) e) 4 :
3 a, b (c d).
,
.
:
(+ 2 3) ; 3-x
((((() 2) 3) )) 5) ; 2-x
( )
; 6
(-77
( )
( )
(- ?)
( NIL)
) ; 5
3

, ,
.
7

,
, (). , (+ 2 3)
,
5, .
QUOTE ( ). , ,
, ,
.
> (+ 2 3)
(+ 2 3)
> (+ 2 3)
5

2.
f (x1 , . . . , xn )
(f x1 x2 ...xn). , .
, ,
: (f x) f (x), (g x y) g(x, y),
(h x (g y z)) h(x, g(y, z)) ..
2.1. . ()
( ).
 CAR (FIRST) :
(CAR <>)
> (car ( ))

> (car (() ))


()
8

> (car ())


NIL
> (car (NIL))
NIL
> (car (NIL))
: NIL
> (car NIL)
NIL
> (car -)
: -

 CDR (REST) :
(CDR <>)
> (cdr ( ))
( )
> (cdr (() ))
()
> (cdr (1))
NIL
> (cdr nil)
NIL

 CONS s- :
(CONS <> <>)
> (cons a (b c))
(A B C) ;
9

> (cons (a b c) nil)


((A B C)) ;
> (cons nil (a b c))
(NIL A B C) ;NIL
> (cons nil nil)
(NIL) ; , NIL

 CAR CDR. CAR CDR


(C...R <>), A ( CAR) D ( CDR).
, (cAr (cDr (cAr x))) (cADAr x).
> (cadr ( !))

> (caddr ((a b c) (d e) (f)))


(F)
> (cadar (cons (a b) nil))
B
> (cddr ((a b c) (d e) (f)))
((f))

CAR, CADR, CADDR, CADDDR ..


FIRST, SECOND,
THIRD, FOURTH, . . . , TENTH.
NTH, n- :
(NTH <n> <c>)


LAST:
(LAST <c>)
10

> (nth 2 (1 2 3))


2
> (third (cons 1 (cons 2 (cons 3 nil))))
3
> (fourth (1 2 3))
NIL
> (last (1 2 ))
()

 LIST
:
(LIST <x1> <x2> <x3> ...)
> (list 1 2)
(1 2)
> (list a b (+ 1 2)) ;
(A B 3)
> (list a (b c) d)
(A (B C) D)
> (list (list a) b nil)
((A) B NIL)
> (list nil)
(NIL)

 4 ATOM .
(ATOM <s->)
4

, T
NIL, .
11

> (atom x)
T
> (atom ( ))
NIL
> (atom (car (1 2 3)))
T

 EQ :
(EQ <-1> <-2>)

EQ T NIL, T ,
. EQ
, , ,
.
( ),
EQ .
EQ .
. , 3.0000, 3 0.3E1 , .
> (eq x y)
NIL
> (eq x (car (x y)))
T
> (eq () nil)
T
> (eq t t)
T

12

> (eq t (atom ))


T
> (eq (a b c) (a b c))
NIL
> (eq 3.14 3.14)
NIL

 EQL , EQ, ( ):
(EQL <-1> <-2>)
> (eql 3 3)
T
> (eql 3.0 3.0)
T
> (eql 3 3.0)
NIL

EQL .
 = :
(= <-1> <-2>)
> (= 3 3.0)
T
> (= 3.00 0.3e1)
T
> (= 3 3 4 5)
NIL
13

 NUMBERP :
(NUMBERP <s->)
> (numberp 3e-11)
T
> (numberp t)
NIL

 NULL :
(NULL <>)
> (null ())
T
> (null (cddr (a b c)))
NIL
> (null nil)
T

 EQUAL :
(EQUAL <s--1> <s--2>)

EQUAL EQL
.
> (equal x x)
T
> (equal (a b c) (a b c))
T
> (equal (a b c) (cons a (b c)))
T
> (equal (nil) ((nil)))
NIL
14

 EQUALP :
(EQUALP <s--1> <s--2>)

EQUALP , ,
. ,
.
EQUALP ,
, .
2.2. . ,
, ,
, , .
- . , ( T NIL) .
. , .
> t
T
> t
T
> nil
NIL
> 3.1415926
3.1415926
15

. - , .
>
: !

 SET :
(SET <> <>)

SET (set)
(bind) .
> (set (car cdr cons atom eq))
(CAR CDR CONS ATOM EQ)

(CAR CDR CONS


ATOM EQ) , , , , SET
.
.
>
(CAR CDR CONS ATOM EQ)

, SET
, .
> (set (car (a b c)) (cdr (d e f g))) ;A := (E F G)
(E F G)
> a ;
(E F G)
> (set (car ) (a b c)) ;CAR := (A B C)
(A B C)
> car ; CAR
(A B C)

, .
16

. SYMBOL-VALUE,
,
:
> (set a (+ 2 3)) ;A := (+ 2 3)
(+ 2 3)
;
> (symbol-value (car (a b c d e)))
(+ 2 3)
> (car (a b c d e))
A

 SETQ SET ( Q):


> (setq (car cdr cons atom eq))
(CAR CDR CONS ATOM EQ)
> (setq a (+ 2 3)) ;a := 5
5

SETQ :
 BOUNDP :
(BOUNDP <>)
> (boundp )
NIL
> (boundp )
T
(boundp t) ;
T
17

 SETF ,
5
.
,
SYMBOL-VALUE ,
.
(SETF <-> <>)

SETF SET SETQ:


(setq x y) <=> (setf x y)
(set x y) <=> (setf (symbol-value x) y)
> (setf (a b c))
(A B C)
>
(A B C)

2.3. . SET,
SETQ, SETF ,
, ,
,
. ,

, (,
) .
,

. , ,
.
, .
5

[1].
18

2.4. EVAL.
EVAL , .
EVAL ,
.
, EVAL : , . !
- ,
.
, , ( QUOTE)
( ).
> (quote (+ 2 3))
(+ 2 3)
>(eval (quote (+ 2 3)))
5
> (setq
(A B C)
> x
X
> (eval
(A B C)
> (eval
:

x (a b c))

x)
x) ; x (A B C)
A

> (eval (quote (quote (a b c))))


(A B C)
> (quote (eval x))
(EVAL X)

, EVAL QUOTE
.
19

3.

- , :
lambda(x1 , x2 , . . . , xn ).fn .
-
(LAMBDA (x1 x2 ... xn) fn)

LAMBDA ,
, xi fn. -.
, : , .
,
(lambda (x y) (+ x y))

- (). ,
,
- :
(- a1 a2 ... an)

ai , , . :
> ((lambda (x y) (+ x y)) 2 3)
5
> ((lambda (x y) (+ x y)) (+ 1 2) 3)
6

-, - , .
20

.
. , -, . , , , -.
-. ,
-
-, :
> ((lambda (y) ; -
((lambda (x) (list y x)) ; -
)) )
( )
;-, -
> ((lambda (x) (list x))
((lambda (y) (list y)) ))
( )

- ,
. .
,

, , .
- , .
DEFUN:
(DEFUN <> <-> <>)


(DEFUN <> <->)

- LAMBDA.
21

DEFUN -, () - .
:
> (defun sum (x y) (+ x y))
SUM
> (sum 2 3)
5
> (defun ( )
(* (/ ) 100))

> ( 5 20)
25

 FBOUNDP :
(FBOUNDP <>)

 SYMBOL-FUNCTION
:
(SYMBOL-FUNCTION <>)
> (fboundp sum)
T
> (symbol-function sum)
(LAMBDA (X Y) (+ X Y))

,
, , (, ).
,
, .
22

.
.
> (setq sum 10)
10
> (sum sum 11)
21

3.1. -.
DEFUN-
,
:
(optional) ;
,
;
(key) ;
(auxiliary) .
(default value).
&,
-. .
:
&OPTIONAL
&REST
&KEY
&AUX

, -
, . &OPTIONAL
. NIL , . ,
23

X Y
(+ X 2):
> (defun f (x &optional (y (+ x 2))) (list x y))
F
> (f 2)
(2 4)
> (f 3 7)
(3 7)

:
> ((lambda (x &optional (y (+ x 2))) (list x y)) 2 5)
(2 5)

, &REST, , .
.
:
> (defun f (x &optional y &rest z) (list x y z))
F
> (f a)
(A NIL NIL)
> (f a b c d)
(A B (C D))
> (f a b (+ 2 3) d)
(A B (5 D))

, &REST-
, QUOTE.
,
, &KEY, .
,
24

, :X. .
, :
> (defun f (&key x y (z 3)) (list x y z))
F
> (f :y 2 :x 1)
(1 2 3)
> (f :z 1)
(NIL NIL 1)

. , ,
.
, QUOTE, SETQ DEFUN,
[1].
4.
, .
: , , -, . , , QUOTE SETQ, , . -
.
:
1. . , , , . ,
T NIL, , . , &KEY -,
.
2. .
25

3. :
a) -;
) , QUOTE
SETQ ,
;
) .
4.1. .
(if else) (for, while, switch)
.
() .
( ) ,
, . ,
, ,
.
, -.
:
:
QUOTE ;
-;
LET LET*.
:
PROG1, PROG2 PROGN.
:
COND, IF, WHEN, UNLESS;
CASE.
26

:
DO, DO*, LOOP, DOTIMES,
DOUNTIL.
:
PROG, GO, RETURN.
:
THROW CATCH, BLOCK.
 LET . :
(LET ((m1 1) (m2 2) ...)
1 2 ...)

LET : m1, m2,... ()


1, 2, ...
1, 2, ...
. m1, m2, ...
.
> (setq x 2)
2
> (let ((x 0))
(setq x 1))
1
> x ; LET X
2
;

LET -, .
(LET ((m1 a1) (m2 a2) ... (mn an)) 1 2 ...)

((LAMBDA (m1 m2... mn) 1 2 ...) a1 a2 ... an)


27

-
, ,

-.
LET . , mi , .. , . :
> (let ((x 2) (y (* 3 x))) (list x y))
: X

LET* LET, :
> (let* ((x 2) (y (* 3 x))) (list x y))
(2 6)

 PROG1, PROG2 PROGN


:
(PROG1 1 2 ... N)
(PROG2 1 2 ... N)
(PROGN 1 2 ... N)

,
(PROG1), (PROG2)
(PROGN) . :
> (progn (setq x 2) (setq y (* 3 x)))
6
> (prog1 (setq x 2) (setq y (* 3 x)))
2
> y
6
28

 COND :
(COND (p1 a1)
(p2 a2)
...
(pN aN))

pi ai . COND
:
1. pi, , ( ) ,
pj, NIL, .. .
2. aj, , COND.
3. , COND
NIL.
T, ,
. COND
:
> (defun solve (a b c &aux (d (- (* b
(cond ((< d 0) no-roots)
((= d 0) (list x= (/ (- b)
(T (list x1= (/ (- (- 0 b)
x2= (/ (+ (- 0 b)
> (solve 1 0 1)
NO-ROOTS
> (1 2 1)
(X= -1)
> (2 3 -4)
(X1= -2.350781 X2= 0.8507811)
29

b) (* 4 a c))))
(* 2 a))))
(sqrt d)) (* 2 a))
(sqrt d)) (* 2 a))))))

aj
:
(COND
...
(pi)
;
...
;
(pk ak1 ak2 ... akN) ;
...)
;

, COND
. ,

COND .

 IF, WHEN, UNLESS CASE.


COND :
(IF - -)

(COND ( -) (T -))

(WHEN 1 2 ...)

(UNLESS (NOT ) 1 2 ...)

(COND ( 1 2 ...))

(IF (PROGN 1 2 ...) NIL)


30

CASE :
(CASE
(-1 m11 m12 ...)
(-2 m21 m22 ...)
...
(otherwise m1 m2 ...))

,

-i.
, mi1, mi2, ...
CASE.
-, otherwise.
 DO
.
:
1. ;
2. , ;
3. (, .);
4. , .
DO :
(DO ((var1 1 1) (var2 2 2) ...)
(- 11 12 ...)
21
22
...)

var1, var2, ..., 1, 2, ..., 1, 2, ... DO , LET. , , NIL.


31


. NIL, 1i, DO.
2i DO. vari () i, , ..
, .
DO
.
> (defun power (x n)
(do (( 1))
;
((= n 0) )
;
(setq (* x));
(setq n (- n 1))))
> (power 2 8)
256

DO , POWER :
> (defun power (x n)
(do (( x (* x))
( n (- 1)))
((= 1) ))) ;
> (power 10 3)
1000

PROG, GO RETURN ( ).
- ,
32

DO. PROG ,
, . ,
PROG DO ,
, .
( [1]),
:
> (defun power (x n)
(prog ()
(setq x)
loop
;
(if (= n 1)
(return ))
;
(setq (* x))
(setq n (- n 1))
(go loop)))
;

.

(LOOP 1 2 ...)

, i
,
RETURN ( , ).
.
DOTIMES DOLIST.
; 1 n
> (defun create-list (n)
(let (list)
; list := nil
(dotimes (number n list)
(setq list (cons (- n number) list)))))
> (create-list 10)
(1 2 3 4 5 6 7 8 9 10)
33

;
> (defun sum (list)
(let ((result 0)) ; result := 0
(dolist (element list result)
(setq result (+ result element)))))
> (sum (1 2 3 4))
10

MAP-, . ,

.
 CATCH THROW ,
, , .
, , , -
, ,
.

.

CATCH:
(CATCH 1 2 ...)

,
1, 2, ... .
,
THROW:
(THROW )
34

THROW
, CATCH, CATCH
THROW.
> (defun div (x y)
(catch error
(cond
((= y 0) (throw error div-by-zero))
(T (/ x y)))))
> (div 1 2)
1/2
> (div 4 0)
DIV-BY-ZERO

5.
5.1. .
> (defun app (list1 list2)
(cond ((null list1) list2)
((null list2) list1)
(t (cons (car list1) (app (cdr list1) list2)))))
> (app (1 2 3) (a b c))
(1 2 3 A B C)

5.2. ,
.
> (defun solve (lst)
(cond
((null lst) nil)
((atom (car lst)) (cons (car lst) (solve (cdr lst))))
(T (append (solve (car lst)) (solve (cdr lst))))))
> (solve (a (b c) a b (c (c))))
(A B C A B C C)
35

5.3.
:
.
> (defun sep (list)
(let ((result) (pos) (neg))
(dolist (value list result)
(if (>= value 0)
(setq pos (app pos (list value)))
(setq neg (app neg (list value)))))
(setq result (list pos neg))))
> (sep (1 -2 3 4 -5))
((1 3 4) (-2 -5))

5.4. CDRN ,
(CDRN N L) (CDD...DR L),
N D.
> (defun cdrn (n list)
(cond
((null list) nil)
((> n 0) (cdrn (- n 1) (cdr list)) )
(T list) ))
> (cdrn 7 (1 2 3 4 5 6 7 8 9))
(8 9)

5.5. .
> (defun inf-to-pref (exp)
(cond
((null exp) nil)
((atom exp) exp)
(t (cons (second exp) (cons (inf-to-pref (first exp))
(list (inf-to-pref (third exp))))))))
> (inf-to-pref (0 + ((a + 4) * ((b - f) + (1 * c)))))
(+ 0 (* (+ A 4) (+ (- B F) (* 1 C))))
> (eval (inf-to-pref ((2 * 3) + (3 * 6))))
24

36

5.6.
.
;
> (defun prefix (lst n)
(cond
((null lst) nil)
(T (do ((i 0 (1+ i))
(lst lst (cdr lst))
(result nil (app result (list (car lst)))))
((eql i n) result)))))
;
(defun shift-left (list n)
(cond
((null list) nil)
(T (app (cdrn n list) (prefix list n) ))))
> (shift-left (1 2 3 4 5 6) 2)
(3 4 5 6 1 2)

5.7. ,
.
> (defun delete-pairs (list)
(cond
((null list) nil)
((null (cdr list)) list)
(T
(cond
((eq (first list) (second list))
(cons (first list)
(delete-pairs (cddr list))))
(T (cons (first list)
(delete-pairs (cdr list))))))))
> (delete-pairs (1 2 2 4 4 6 6))
(1 2 4 6)

37


1. . . T.1. / . , . . .: ,
1990. 447 c.
2. . . T.2. / . , . . .: ,
1990. 318 c.
3. Seibel P. Practical Common Lisp. / Peter Seibel. Apress, 2005. 528 p.
4. Abelson H. Structure and Interpretation of Computer Programs / Harold
Abelson, Gerald Jay Sussman and Julie Sussman. MIT Press, 1996.
683 p.
5. Allen J. Anatomy of Lisp / Jhon Allen. Mcgraw-Hill College, 1980.
464 p.
6. Steele G. L. Common Lisp the Language, 2nd edition / Steele Guy L.
Digital Press, 1990 1029 p.
7. Harrison J. Introduction to Functional Programming / John Harrison.
1996. :
http://www.cl.cam.ac.uk/teaching/Lectures/funprog-jrh-1996/index.html

38

-
.

4
5
6.040302
-

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

: . .
22.03.2012 60x84/16. .
2,3 .. 50

95007, , . , 4.
. ..