xxxxxxxxxx101factorial(0,1).23factorial(A,B) :-4A > 0,5C is A-1,6factorial(C,D),7B is A*D.89?- factorial(10,What).10What = 3628800.
xxxxxxxxxx181takeout(A,[A|B],B).2takeout(A,[B|C],[B|D]) :-3takeout(A,C,D).45?- takeout(X,[1,2,3,4],Y).6X = 1,7Y = [2, 3, 4] ;8X = 2,9Y = [1, 3, 4] ;10X = 3,11Y = [1, 2, 4] ;12X = 4,13Y = [1, 2, 3] ;14false.1516?- takeout(X,[1,2,3,4],_), X>3.17X = 4 ;18false.
xxxxxxxxxx311move(1,X,Y,_) :-2write('Move top disk from '),3write(X),4write(' to '),5write(Y),6nl.7move(N,X,Y,Z) :-8N>1,9M is N-1,10move(M,X,Z,Y),11move(1,X,Y,_),12move(M,Z,Y,X).1314?- move(4,left,right,center).15Move top disk from left to center16Move top disk from left to right17Move top disk from center to right18Move top disk from left to center19Move top disk from right to left20Move top disk from right to center21Move top disk from left to center22Move top disk from left to right23Move top disk from center to right24Move top disk from center to left25Move top disk from right to left26Move top disk from center to right27Move top disk from left to center28Move top disk from left to right29Move top disk from center to right30true ;31false.
xxxxxxxxxx101edgelist02 <- c(1,2,1,3,1,4,2,5,2,6,3,7,7,11,7,12,4,8,4,9,4,10)2ne <- length(edgelist02)/23nv <- max(edgelist02)4g02 <- graph(edgelist02,directed=T)5g02 <- set_vertex_attr(g02, "label", value = letters[1:nv])6g02 <- set_vertex_attr(g02, "label.degree", value = c(-pi/2,rep(0,nv-1)))7g02 <- set_vertex_attr(g02, "label.dist", value = rep(2,nv))8g02 <- set_vertex_attr(g02, "label.cex", value = rep(1.2, nv))9g02 <- set_edge_attr(g02, "color", value = rep("#006400",ne))10plot(g02,layout=layout_as_tree)
定义几个操作符
xxxxxxxxxx41:- op(500,xfx,'is_parent').2:- op(500,xfx,'is_sibling_of').3:- op(500,xfx,'is_same_level_as').4:- op(500,xfx,'has_depth').
创建tree数据库
xxxxxxxxxx51a is_parent b. a is_parent c. a is_parent d.2b is_parent e. b is_parent f.3c is_parent g.4d is_parent h. d is_parent i. d is_parent j.5g is_parent k. g is_parent l.
定义"is_sibling_of"
xxxxxxxxxx31X is_sibling_of Y :- Z is_parent X,2Z is_parent Y,3X \== Y.
定义"is_same_level_as"
xxxxxxxxxx41X is_same_level_as X .2X is_same_level_as Y :- W is_parent X,3Z is_parent Y,4W is_same_level_as Z.
定义"has_depth",这里用到了cut技术,即"!",将在后面介绍。
xxxxxxxxxx51a has_depth 0 :- !.2Node has_depth D :-3Mother is_parent Node,4Mother has_depth Dp,5D is Dp + 1.
定义"path"(path是一条从根到该节点的路径)
xxxxxxxxxx91path(Node) :-2parent_path(Node),3write(Node).4parent_path(a).5parent_path(Node) :-6Mother is_parent Node,7parent_path(Mother),8write(Mother),9write(' --> ').
定义“height” (height是从该节点到其下某个叶子的最大路径长度)
xxxxxxxxxx61height(N,H) :- setof(Z,ht(N,Z),Set),2max(Set,0,H).3ht(Node,0) :- leaf(Node), !.4ht(Node,Hn) :- Node is_parent Child,5ht(Child,Hc),6Hn is Hc+1.
setof是一个内置谓词(参见Predicate setof/3), 搜集各个分支的高度汇总到Set变量中。
定义"leaf"(叶子节点)
xxxxxxxxxx11leaf(Node) :- not(Node is_parent Child).
定义"max"
xxxxxxxxxx31max([],M,M).2max([X|R],M,A) :-3(X > M -> max(R,X,A) ; max(R,M,A)).
目标
xxxxxxxxxx421?- max([3,9,8],7,X).2X = 9.34?- max([3,9,11,8],14,X).5X = 14.67?- leaf(f).8true.910?- leaf(g).11false.1213?- X is_sibling_of i.14X = h ;15X = j ;16false.1718?- X is_same_level_as c.19X = c ;20X = b ;21X = c ;22X = d ;23false.2425?- k has_depth X.26X = 3.2728?- parent_path(k).29a --> c --> g -->30true ;31false.3233?- path(k).34a --> c --> g --> k35true ;36false.3738?- height(c,X).39X = 2.4041?- height(k,X).42X = 0.