Вы находитесь на странице: 1из 29

# ..-..

,
dmitryso@microsoft.com

( )

2009 ..

uniq_factors(X),
, X

unique_factors(X) :- factor(X,L), uniq(L).
factor(X,L) :- factor(2,X,[],L).
factor(N,X,L,R) :- X mod N =\= 0, N1 is N+1, N1=<X, factor(N1,X,L,R).
factor(N,X,L,R) :- 0 is X mod N, X1 is X//N, factor(N,X1,[N|L],R).
factor(N,X,L,L) :- N>X.

2009 ..

uniq(L)
uniq([]).
uniq([X|T]) :- not_member(X,T), uniq(T).

not_member(X,[]).
not_member(X,[Y|T]):- X \= Y, not_member(X,T).

uniq([]).
uniq([X|T]) :- not(member(X,T)), uniq(T).

2009 ..

not(P):
, P

,
P

5
2009 ..

2009 ..

- ,

()

( A) ( A)
6

2009 ..

p :- p.

q :- q.
q.

not(p) not(q)

,
,
.

parent(X,Y).
parent(X,Z),
ancestor(Z,Y).

2009 ..

ancestor(X,Y) :ancestor(X,Y) :-

(parent(X,Z)
ancestor(Z,Y)) ancestor(X,Y)
parent(X,Y)

(parent(X,Z)ancestor(Z,Y))ancestor(X,Y)
parent(X,Y)

(completed
program) comp(P) P
.

2009 ..

1 [
] P
.
A SLD, ,
comp(P) A.
2 [
] P
. comp(P)
A, SLD A,
.
9

2009 ..

## Definite Logic Program ,

:
A , A

,
;
A ,
A .

SLDNF-,
,
10

2009 ..

?-not(parent(X,Y)).

,

?-parent(X,Y) (X) (Y) parent(X,Y)
?-not(parent(X,Y)) ((X) (Y)
parent(X,Y)) = (X)(Y) parent(X,Y)
?-parent(X,_), parent(_,Y), not(parent(X,Y)).

11

2009 ..

?-not(not(parent(X,Y))).

,

yes.

12

2009 ..

sweet(sugar).
white(sugar).
white(paper).
?- white(X), not(sweet(X)).
?- not(sweet(X)),white(X).

-

,
,

, ..

.

13

2009 ..

parent(X) :- parent(X,_).
father(X) :- parent(X), male(X).

14

2009 ..

parent(X) :- parent(X,_),!.
father(X) :- parent(X), male(X).

15

2009 ..

,
(.. ),
.
,
-
( ),

.

C :- P, Q, !, R, S.
C :- T.
A :- B, C, D.
16

2009 ..

## father(X,Y) :- parent(X,Y), male(X).

?- father(X,tatyna).

## father(X,Y) :- parent(X,Y), male(X),!.

?- father(X,tatyna).

17

2009 ..

father(X,Y) :- parent(X,Y), male(X),!.
?- father(nicolas_ii,X).

18

2009 ..

subset([],_).
subset([X|T],L) :- member(X,L), subset(T,L).

member
,
.
subset([],_).
subset([X|T],L) :- included(X,L), subset(T,L).
included(X,[X|_]) :- !.
included(X,[_|T]) :- included(X,T).

19

2009 ..

passed(ivanov) :- !, fail.
passed(_).

:
passed(X) :- not(X=ivanov).

, ?
?-passed(X).
?-student(X),passed(X).

20

2009 ..

!-fail not!
not(P) :- P, !, fail.
not(_).

! ,

?
!
not ,

not !
.

21

2009 ..

num_legs(centipede,40) :- !.
num_legs(human,2) :- !.
num_legs(_,4).
?- num_legs(centipede,X).
?- num_legs(dog,X).
?- num_legs(centipede,4).

num_legs(centipede,X) :- !,X=40.
num_legs(human,X) :- !,X=2.
num_legs(_,4).

22

2009 ..

Q :- if A then B else C.

Q :- A, !, B.
Q :- C.

(+)
(-)
( )

Q :- A, B.
Q :- not(A), C.

(A
)
(+) (
)

(-)

23

Q :- A --> B ; C.
Q :- (

2009 ..

Q :- if A then B else C.

## :- func fact(int) = int.

:- mode fact(in) = out is det.
fact(N) = R :N=1 --> R=1;
R = N*fact(N-1).

A1,B1,C1,;
A2,B2,C2,;

).

Mercury

.
24

2009 ..

## fact(1,1).fact(N,F) :- not(N=1), N1 is N-1, fact(N1,F1), F is F1*N.

fact(1,1).
fact(N,F) :N1 is N-1,
fact(N1,F1),
F is F1*N.

X
fact(1,1) :- !.
fact(N,F) :N1 is N-1,
fact(N1,F1),
F is F1*N.
fact(1,1).
fact(N,F) :not(N=1),
N1 is N-1,
fact(N1,F1),
F is F1*N.

fact(N,1) :- N=1.
fact(N,F) :not(N=1),
N1 is N-1,
fact(N1,F1),
F is F1*N.

## fact(N,F) :N=1 --> F=1;

N1 is N-1,
fact(N1,F1),
F is F1*N.

fact(1,1).
fact(N,F) :N>1,
N1 is N-1,
fact(N1,F1),
F is F1*N.
25

.
.

26

2009 ..

,
,
.
- ,
.

lucky(L) :append(A,B,L),
sum(A,S), sum(B,S).

lucky(L) :append(A,B,L),
sum(A,S1),
sum(B,S2),
S1 = S2.

27

2009 ..

solve(N,L) :integers(0,9,D),
genlist(N,D,L),
lucky(L).
genlist(0,_,[]).
genlist(N,L,[A|R]) :- N>0,
N1 is N-1,
genlist(N1,L,R),
member(A,L).
integers(A,A,[A]).
integers(A,B,[A|R]) :A<B, A1 is A+1,
integers(A1,B,R).

28

2009 ..

generate(N,L) :integers(0,9,D),
genlist(N,D,L),
test(L) :- lucky(L).
solve(N,X) :generate(N,X),
test(X).

29

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2022 Scribd Inc.

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2022 Scribd Inc.