LISP语言

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1、函数型语言程序:函数集没有“主函数”、各函数可独立运行函数:由其它函数构成(无语句概念)注:特殊函数的参数不求值。如setq,I/O等5.1.1LISP语言特点2、数据与程序的一致性(defuninference(factsrules)(do((rule_listrules(cdrrule_list))((nullrule_list)(returnnil))(cond((try_rule(carrule_list))(returnt)))))(Rule_6(if(Integerx)(GZx))(then(Naturalx)))3、数据类型唯一(基本LISP)数原子原子S-表达式文字原子表5.1.1LISP语言特点例函数定义:(defunhanoi(xyzn)(cond((=n1)(move_diskxy))(T(hanoixzy(-n1))(move_diskxy)(hanoizyx(-n1)))))函数调用:(hanoi'A'B'C3)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)move(a,2,b)hanoi(1,c,a,b)move(c,1,b)调用格式:(函数名参数1参数2……)返回值1、算术函数+-*/①(+135)②(-135)9-7③(*135)④(/135)151/155.1.2LISP系统函数简介2、不求值、赋值、再求值函数①不求值函数guote(guotetry)'try②赋值(set'A'B)A==B(setA10)B==10(setqA10)A==10③再求值(二次求值)(setqA'B)A==B(setqB'C)B==C(evalA)C(setqM(+25)M==7(setqN'(+25)N==(+25)(evalN)7(firstN)+3、建表函数①list(setqL1(list'A'B'C))L1==(ABC)(setqL2(listL1'is'a'list))L2==((ABC)isalist)②cons(setqL(cons'adding'(aelement))L==(addingaelement)③append(setqL1'(Listone))L1==(Listone)(setqL2'(listtwo))L2==(listtwo)(setqL(appendL1L2)L==(Listonelisttwo)4、分解表函数①first或CAR(first'(ABC))(CAR'(ABC))AA②rest或CDR(rest'(ABC))(CDR'(ABC))(BC)(BC)(CDR(CDR'(ABCD)))(CD)(CDDR'(ABCD))(CD)(CAR(CDDR'(ABCD)))C(CADDR'(ABCD))C(CAR(CDR'(ABCD)))B(second'(ABCD))B(CAR(CDDR'(ABCD)))C(third'(ABCD))C(nth1'(ABCD))B(second'(ABCD))B(nth3'(ABCD))C(third'(ABCD))C已知规则R:(setqR'(Rule4(if(animalflies)(animallayseggs))(then(animalisbird))))取规则名:(CARR)Rule4(setqR'(Rule4(if(animalflies)(animallayseggs))(then(animalisbird))))取前提:(setqx(cdrR))x==((if…)(then…))(setqy(carx))y==(if△△)(setqz(cdry))z==(△△)(setqR'(Rule4(if(animalflies)(animallayseggs))(then(animalisbird))))取前提:(setqz(cdadrR))练习给出结果:(setqA'B)(setqN'(setA'(+58)))(evalN)(setqR'(Rule4(if(animalflies)(animallayseggs))(then(animalisbird))))练习:用second、third、rest等求前提。(setqz(rest(secondR)))(setqR'(Rule4(if(animalflies)(animallayseggs))(then(animalisbird))))练习:用second、third、rest等求前提:(setqz(rest(secondR)))(setqR'(Rule4(if(animalflies)(animallayseggs))(then(animalisbird))))练习:用second、third、rest等求结论。(setqc(second(thirdR)))(setqR'(Rule4(if(animalflies)(animallayseggs))(then(animalisbird))))练习:用second、third、rest等求结论。(setqc(second(thirdR)))5、逻辑函数(1)(atom‘(abc))nil(atom(third‘(abc)))t(null‘(abc))nil(null‘())t5、逻辑函数(2)(equal(abc)(abc))t(equal()nil)t(equal(abc)(a(bc)))nil5、逻辑函数(3)(and(atom())(null(cdr(a)))))t(or(listp())nil)t(not(equal()nil))nil6、条件函数(3)(cond(exp11exp12…)(exp21exp22…)(expn1expn2…))(cond((equalanswer'y)1)((equalanswer'n)-1)(t(print“Enteryorn)(askquery)))7、定义函数的函数例:定义一个求表的第二个元素的函数(defunsecond(l)(cond((atoml)nil)(t(cadrl))))(second'(A(BC)D))(BC)例:定义前述取规则前提函数(defunget_if(Rule)(cdadrRule))递归例:求mnm0=1mn=m×mn-1(n>0)(defunpower_1(mn)(cond((=n0)1)(t(*m(power_1m(-n1))))))调用函数的函数:funcall(funcall‘函数名'参数1……'参数n)例:(funcall'+23)5(funcall'append'(ab)'()'(c)))(abc)

1 / 31
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功