Académique Documents
Professionnel Documents
Culture Documents
. .
tree (Element, Left, Right), Element, Left, Right), ElementElement, , a Left RightRight. void. void. a b c
2
tree( tree(a, tree (b, void, void), void), tree (c, void, void)). void)). . binary_tree (Tree) :- binary_tree(void). :binary_tree (tree (Element, Left, Right)) ::binary_tree (Left), binary _tree(Right). , . . , . .
3
Tree. Tree. : -
treetree-member (Element,Tree) :tree_member (X, tree(X, Left, Right)). tree(X, tree_member (X, tree(Y, Left, Right)) :tree_member(X,Left). tree_member(X,tree(Y,Left,Right)) :tree _ member(X,Right).
, . , . T1 2 .
6
, ,
2 T1
a b c b . c
b a c ,
. . , , / ,
.
8
isotree(Tree1,Tree2), isotree(Tree1,Tree2), r 1
, 2. .
isotree (void, void). isotree(tree (X, Left1 ,Right1), tree(X,Left2,Right2)) ::isotree(Left1, Left2), isotree(Right1, Right2). isotree(tree (X, Left1, Right1), tree(X, Left2, Right2)) ::isotree(Left1,Right2), isotree(Right1,Left2).
9
, , . . : , , ; ; . , , , epx, epx,
. , , .
10
% PrePreTree . pre_ pre_order(tree(X, L, R), Xs) ::pre_order(L, Ls), pre_order(R,Rs), ( [X | Ls], Rs, Xs). pre_order(void, [ ]). % In Tree . in_order(tree(X, L, R),Xs) ::in_order(L, Ls), in_order(R, Rs), (Ls, [X | Rs], Xs). in_order(void,[ ]). % PostPostTree post_order(tree(X,L,R),Xs) :post_order(L,Ls), post_order(R,Rs), (Rs, [X], Rs1), Rs1 (Ls, Rs1, Xs). Rs1 post_order(void,[ ]). .
11
. , . : , .
12
, , . , , 3 . . n 2^n -1 .
13
. ,
64
Moves N - A, .
hanoi( s(0), A, B, C, [A to ]). hanoi (s(N), A, B, C, Moves) ::hanoi( N, A, C, B, Ms1), hanoi(N, C, B, A, Ms2), (Ms1, [A to | Ms2], Moves). (Ms1
15
, Moves s(N) , Ms1 Ms1 N , Ms2 Ms2 , N , Moves [ to B\Ms2] B\Ms2 Ms1. Ms1. , :
16
, Moves s(N) , Ms1 N , Ms2 Ms2 , N , Moves [ toB \ Ms2] Ms2] Ms1. Ms1. , :
17
. ,
, . :
18
19
F
F = 'true' 'true' F = X & Y, X F = X I Y, Y, F = ~ X, X F = 'false' 'false' F = X & Y, F = X I Y, X Y, F = ~ X, X Y . Y X .
Y(
,
X Y( )
20
, . , , . . , . .
21
% satisfiable(Formula) satisfiable(Formula) % % Formula. Formula. satisfiable(true). satisftable(X & Y):- satisfiable(X), Y):satisfiable(Y). satisfiable(X I Y):- satisfiable(X). Y):satisfiable(X I Y):- satisfiable(Y). Y):satisfiable( ~ X):- invalid(X). X):22
% invalid(Formula) invalid(Formula) % % Formula. Formula. invalid(false). invalid(X I Y):- invalid(X), invalid(Y). Y):invalid(X & Y):- invalid(X). Y):invalid(X & Y):- invalid(Y). Y):invalid(~ Y):- satisfiable(Y). Y):-
23