xxxxxxxxxx
101factorial(0,1).
2
3factorial(A,B) :-
4A > 0,
5C is A-1,
6factorial(C,D),
7B is A*D.
8
9?- factorial(10,What).
10What = 3628800.
xxxxxxxxxx
181takeout(A,[A|B],B).
2takeout(A,[B|C],[B|D]) :-
3takeout(A,C,D).
4
5?- 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.
15
16?- takeout(X,[1,2,3,4],_), X>3.
17X = 4 ;
18false.
xxxxxxxxxx
311move(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).
13
14?- move(4,left,right,center).
15Move top disk from left to center
16Move top disk from left to right
17Move top disk from center to right
18Move top disk from left to center
19Move top disk from right to left
20Move top disk from right to center
21Move top disk from left to center
22Move top disk from left to right
23Move top disk from center to right
24Move top disk from center to left
25Move top disk from right to left
26Move top disk from center to right
27Move top disk from left to center
28Move top disk from left to right
29Move top disk from center to right
30true ;
31false.
xxxxxxxxxx
101edgelist02 <- 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)/2
3nv <- 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)
定义几个操作符
xxxxxxxxxx
41:- 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数据库
xxxxxxxxxx
51a 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"
xxxxxxxxxx
31X is_sibling_of Y :- Z is_parent X,
2Z is_parent Y,
3X \== Y.
定义"is_same_level_as"
xxxxxxxxxx
41X 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技术,即"!",将在后面介绍。
xxxxxxxxxx
51a has_depth 0 :- !.
2Node has_depth D :-
3Mother is_parent Node,
4Mother has_depth Dp,
5D is Dp + 1.
定义"path"(path是一条从根到该节点的路径)
xxxxxxxxxx
91path(Node) :-
2parent_path(Node),
3write(Node).
4parent_path(a).
5parent_path(Node) :-
6Mother is_parent Node,
7parent_path(Mother),
8write(Mother),
9write(' --> ').
定义“height” (height是从该节点到其下某个叶子的最大路径长度)
xxxxxxxxxx
61height(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"(叶子节点)
xxxxxxxxxx
11leaf(Node) :- not(Node is_parent Child).
定义"max"
xxxxxxxxxx
31max([],M,M).
2max([X|R],M,A) :-
3(X > M -> max(R,X,A) ; max(R,M,A)).
目标
xxxxxxxxxx
421?- max([3,9,8],7,X).
2X = 9.
3
4?- max([3,9,11,8],14,X).
5X = 14.
6
7?- leaf(f).
8true.
9
10?- leaf(g).
11false.
12
13?- X is_sibling_of i.
14X = h ;
15X = j ;
16false.
17
18?- X is_same_level_as c.
19X = c ;
20X = b ;
21X = c ;
22X = d ;
23false.
24
25?- k has_depth X.
26X = 3.
27
28?- parent_path(k).
29a --> c --> g -->
30true ;
31false.
32
33?- path(k).
34a --> c --> g --> k
35true ;
36false.
37
38?- height(c,X).
39X = 2.
40
41?- height(k,X).
42X = 0.