Prolog-程序范例

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

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

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

资源描述

3的阶乘:predicatesfactorial(unsigned,real)clausesfactorial(1,1):-!.factorial(X,FactX):-Y=X-1,factorial(Y,FactY),FactX=X*FactY.goalX=3,factorial(X,Y).输出表domainslist=integer*predicateswrite_a_list(list)clauseswrite_a_list([]).write_a_list([H|T]):-write(H),nl,write_a_list(T).goalwrite_a_list([1,2,3]).统计表元素个数domainslist=integer*predicateslength_of(list,integer)clauseslength_of([],0).length_of([_|T],L):-length_of(T,TailLength),L=TailLength+1.goallength_of([1,2,3],L).每个元素加1domainslist=integer*predicatesadd1(list,list)clausesadd1([],[]).add1([Head|Tail],[Head1|Tail1]):-Head1=Head+1,add1(Tail,Tail1).goaladd1([1,2,3,4],NewList).删除整数表中的负数domainslist=integer*predicatesdiscard_negatives(list,list)clausesdiscard_negatives([],[]).discard_negatives([H|T],ProcessedTail):-H0,!,discard_negatives(T,ProcessedTail).discard_negatives([H|T],[H|ProcessedTail]):-discard_negatives(T,ProcessedTail).goaldiscard_negatives([2,-45,3,468],X).判断表成员domainsnamelist=name*name=symbolpredicatesmember(name,namelist)clausesmember(Name,[Name|_]).member(Name,[_|Tail]):-member(Name,Tail).goalmember(susan,[ian,susan,john]).合并表domainsintegerlist=integer*predicatesappend(integerlist,integerlist,integerlist)clausesappend([],List,List).append([H|L1],List2,[H|L3]):-append(L1,List2,L3).goalappend([1,2,3],[5,6],L).输出表中元素domainsintegerlist=integer*namelist=symbol*predicateswritelist(integerlist)writelist(namelist)clauseswritelist([]).writelist([H|T]):-write(H,),writelist(T).goalwritelist([1,2,3,4]),nl.找出一个数字链表的最大数值。domains/*领域段*/list=integer*.predicates/*谓词段*/max(list,integer)clauses/*子句段,存放所有的事实和规则*/max([H],H).max([H|T],H):-max(T,X),H=X.max([H|T],X):-max(T,X),HX.动态生成关于N个学生(包括学号、姓名、出生年月日、成绩)的内部事实数据库,并计算学生成绩的平均分。domainsnum=symbolname=symbolbirthday=date(integer,integer,integer)score=reallist=score*factsstud(num,name,birthday,score)predicatesrunadd_fact(integer)sumlist(list,real)clausesrun:-write(entertheN:),nl,readint(N),add_fact(N),findall(Score,stud(_,_,_,Score),Slist),sumlist(Slist,Sum),Ave=Sum/N,nl,write(theaveragescoreis:,Ave).add_fact(N):-N=1,!,nl,write(the,N,student:),nl,write(num:),readln(Num),write(name:),readln(Name),write(year:),readint(Year),write(month:),readint(Month),write(date:),readint(Date),write(score:),readreal(Score),assertz(stud(Num,Name,date(Year,Month,Date),Score)),N1=N-1,add_fact(N1).add_fact(N):-N1.sumlist([],0).sumlist([H|T],Sum):-sumlist(T,S1),Sum=H+S1.goalrun.实现表的插入排序,要求表中的N个元素由键盘读入domainselement=integerlist=element*predicatesrun(list)insert_N(list,element,list)append(list,list,list)write_list(list)clausesrun(List):-readint(N),N0,!,insert_N(List,N,List1),write_list(List1),nl,run(List1).run(_).insert_N([],N,[N]):-!.insert_N([H|T],N,[H1|T1]):-H=N,!,H1=H,insert_N(T,N,T1).insert_N([H|T],N,[H1|T1]):-HN,H1=N,append([H],T,T1).append([],L2,L2):-!.append([H1|T1],L2,[H1|T3]):-append(T1,L2,T3).write_list([]):-!.write_list([H|T]):-write(H,),write_list(T).goalrun([]).求和domainslist=integer*predicatessumlist(list,integer)clausessumlist([],0).sumlist([H|T],Sum):-sumlist(T,S1),Sum=H+S1.goalsumlist([1,2,3],Sum).求最大值domainslist=integer*predicatesmax(list,integer)clausesmax([H],H).max([H|T],H):-max(T,X),H=X.max([H|T],X):-max(T,X),HX.goalmax([1,2,4],H).求最小值domainslist=integer*predicatesmax(list,integer)clausesmax([H],H).max([H|T],H):-max(T,X),H=X.max([H|T],X):-max(T,X),HX.goalmax([1,2,4],H).求个数domainslist=integer*predicateslength_of(list,integer)clauseslength_of([],0).length_of([_|T],L):-length_of(T,TailLength),L=TailLength+1.goallength_of([1,2,3],L).求平均值predicatesadv(real,real)clausesadv(X,Y):-Diff=X/Y,write(adv=,Diff,'\n').goalX=47,Y=4,adv(X,Y).

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

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

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

×
保存成功