LISP语言

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

(setq R ' (Rule4 (if (animal flies) (animal lays eggs)) (then (animal is bird)) ) )

取前提:
(setq z (cdadr R))
练习
给出结果: (setq A 'B)
(setq N '(set A '(+ 5 8))) (eval N)
(Rule_6
(if (Integer x) (GZ x) ) (then (Natural x) ) )
5.1.1 LISP 语言特点 3、数据类型唯一(基本 LISP) 数原子
原子
S-表达式

文字原子

函数定义: (defun hanoi ( x y z n ) (cond ((= n 1) (move_disk x y)) (T (hanoi x z y (- n 1)) (move_disk x y) (hanoi z y x (- n 1))) ) )
2、不求值、赋值、再求值函数 ①不求值函数guote (guote try) 'try ②赋值 (set 'A 'B) (set A 10) (setq A 10)
A == B B == 10 A == 10
③再求值(二次求值) (setq A 'B) A == B (setq B 'C) B == C (eval A) C (setq M (+ 2 5) M == 7 (setq N '(+ 2 5) N == (+ 2 5)
(second '( A B C D)) B (CAR (CDDR ' (A B C D))) C
(third ' (A B C D)) C
(nth 1 '(A B C D)) B
(second '( A B C D)) B (nth 3 ' (A B C D)) C
(third ' (A B C D)) C
(second ' ( A (B C) D))
(B C)
例:定义前述取规则前提函数
(defun get_if (Rule)
(cdadr Rule) )
递归

例:求mn
m0=1 mn=m×mn-1
(n>0)
(defun power_1 (m n)
(cond ((= n 0) 1)
(t (* m (power_1 m (- n 1)) ) ) ) )
(eval N) 7 (first N) +
3、建表函数 ① list (setq L1 (list 'A 'B 'C)) L1 == (A B C) (setq L2 (list L1 'is 'a 'list)) L2 == ((A B C) is a list)
② cons (setq L (cons 'adding '(a element)) L == (adding a element)
③ append
(setq L1 ' ( List one)) L1 == (List one)
(setq L2 ' (list two)) L2 == (list two)
(setq L (append L1 L2) L == (List one list two)
4、分解表函数
① first 或 CAR (first ' ( A B C )) A ② rest 或 CDR (CAR ' (A B C)) A
5、逻辑函数(3)
(and (atom ( )) (null (cdr (a)) ) ) )
t (or (listp ( )) nil )
t
(not (equal ( ) nil ) )
nil
6、条件函数(3)

(cond (exp11 exp12 … ) (exp21 exp22 … )

函数调用: ( hanoi 'A 'B 'C 3 )

Hanoi ( 3, a, b, c ) 的递归调用过程
hanoi(3, a, b, c)
hanoi(2, a, c, b)
move( a, 3, c)
hanoi(2, b, a, c)
hanoi(1, a, b, c) move( a, 1, c)
(rest ' (A B C)) (B C)
(CDR ' (A B C)) (B C)
(CDR (CDR '(A B C D))) (C D)
(CDDR '( A B C D)) ( C D) (CAR (CDDR ' (A B C D))) C
(CADDR ' (A B C D)) C
(CAR (CDR '(A B C D))) B
(expn1 expn2 … ) )
(cond ((equal answer 'y) 1) ((equal answer 'n) -1)
(t (print “Enter y or n")
(ask query)) )
7、定义函数的函数

例:定义一个求表的第二个元素的函数 (defun second ( l ) (cond ((atom l) nil) (t (cadr l))))
5、逻辑函数(1) (atom ‘(a b c)) nil (atom (third ‘(a b c))) t (null ‘( a b c ) ) nil (null ‘( ) ) t
5、逻辑函数(2)
(equal (a b c) (a b c) ) t (equal ( ) nil ) t (equal (a b c) (a (b c)) ) nil
5.1.1 LISP语言特点
1、函数型语言

程序:函数集 没有 “主函数”、 各函数可独立运行
函数:由其它函数构成(无语句概念)


注:特殊函数的参数不求值。 如setq , I/O等
2、数据与程序的一致性 (defun inference ( facts rules ) (do ( (rule_list rules (cdr rule_list) ) ( (null rule_list) (return nil) ) (cond ( (try_rule (car rule_list) ) ( return t ) ) ) ) )
move( a, 2, b)
hanoi(1, c, a, b) move( c, 1, b)
5.1.2 LISP系统函数简介

调用格式: 返回值
(函数名 参数1 参数2……) 1、算术函数 + - * /
①(+
9
1 1
3 3
5)
②(- 1
-7
3 3
5) 5)
③ (*
15
5) ④ ( / 1
1/15

练习:
用second、third 、rest 等求前提:
(setq z (rest (second R) ) )
(setq R ' (Rule4 (if (animal flies) (animal lays eggs)) (then (animal is bird)) ) )

练习:
用second、third 、rest 等求结论。
(setq R ' (Rule4 (if (animal flies) (animal lays eggs)) (then (animal is bird)) ) )
Байду номын сангаас
练习:
用second、third 、rest 等求前提。
(setq z (rest (second R) ) )
(setq R ' (Rule4 (if (animal flies) (animal lays eggs)) (then (animal is bird)) ) )
(setq c (second (third R) ) )
(setq R ' (Rule4 (if (animal flies) (animal lays eggs)) (then (animal is bird)) ) )

练习:
用second、third 、rest 等求结论。
(setq c (second (third R) ) )

已知规则 R : (setq R ' (Rule4 (if (animal flies) (animal lays eggs))
(then (animal is bird)) ) )

取规则名:
(CAR R)
Rule4
(setq R ' (Rule4 (if (animal flies) (animal lays eggs)) (then (animal is bird)) ) ) 取前提: (setq x (cdr R)) x == ( (if … ) (then…) ) (setq y (car x)) y == (if △ △ ) (setq z (cdr y)) z == (△ △)

调用函数的函数:funcall
(funcall ‘函数名 '参数1 …… '参数n )

例: '+ 2 3)
(funcall 5
(funcall 'append '(a b) '( ) '( c ) ) )
(a b c)
相关文档
最新文档