. , , ,

,,,

Prolog — ,

Prolog

1.

, .

predicates

hello.

goal

hello.

clauses

hello:-

makewindow(1,7,7," ",4,56,14,22),

nl, write(" ,n"," Enter."),

cursor(4,6),

readln(Name),nl,

write(" n PDC Prolog,n","",Name,"!").

: PDC Prolog, Vladimir!

, .

predicates

hello.

goal

hello.

clauses

hello:-

makewindow(1,7,7," ",4,56,14,22)

nl, write(" ,n"," Enter."),

cursor(4,6),

readln(Name)nl,

write(" n PDC Prolog,n",",Name,!").

: 423 Syntax eror.

2.

. .

predicates

book(symbol,symbol,symbol,integer).

clauses

book(" ..","IBM PC ","",1988).

book(" .."," ","",1984).

book(" ..","- ","",1987).

book(" .."," ","",1989).

book(" ..","PDC Prolog","",2000).

book(" .."," ","",1997).

book(" .."," ","",1999).

: X= Proektirovanie OR BD, Y=SUDOSTROENIE, Z=1984

X=Object models of DATA, Y=SZPI, Z=1987

X=Modelirovanie CMO, Y=SZPI, Z=1989

X=PDC Prolog, Y=SZPI, Z=2000

4 Solutions

X= Proektirovanie OR BD

X=Object models of DATA

X=Modelirovanie CMO

X=PDC Prolog

4 Solutions

E=Petuchov O.A., X=Object models of DATA, Z=1987

E=Petuchov O.A., X=Modelirovanie CMO, Z=1989

E=Petuchov O.A., X= PDC Prolog, Z=2000

E=Ankudinov G.I., X=Automat theory, Z=1997

E=Nikolaev V.I., X=Diskretniye struktury, Z=1999

5 Solutions

3.

:

domains

name=symbol

year_in, year_out = integer

predicates

parents(name,name)

woman(name)

man(name)

offspring(name,name)

father(name,name)

mother(name,name)

parent_parents(name,name)

brother(name,name)

grandfather(name,name)

grandmother(name,name)

emperor(name,year_in,year_out)

emperor_was(name,integer)

clauses

parents(" III"," I").

parents(" II"," I").

parents(" I"," I").

parents(" I"," I").

parents(" I"," II").

parents(" II"," III").

parents(" III"," II").

woman(" II").

man(" III").

man(" I").

man(" I").

man(" I").

man(" II").

man(" III").

man(" II").

offspring(Y,X):-parents(X,Y).

father(X,Y):-parents(X,Y),man(X).

mother(X,Y):-parents(X,Y),woman(X).

parent_parents(X,Z):-parents(X,Y),parents(Y,Z).

brother(X,Y):-parents(Z,X),parents(Z,Y),man(X),X<>Y.

grandfather(X,Y):-father(X,Z),father(Z,Y).

grandmother(X,Y):-mother(X,Z),father(Z,Y).

emperor(" III",1761,1762).

emperor(" II",1762,1796).

emperor(" I",1796,1801).

emperor(" I",1801,1825).

emperor(" I",1825,1855).

emperor(" II",1855,1881).

emperor(" III",1881,1894).

emperor(" II",1894,1917).

emperor_was(X,Y):-emperor(X,A,B),Y>=A,Y<=B.

: Y=aleksandr I X=petr III, A=1761, B=1762 X=pavel I

1 Solution X=ekaterina II, A=1762, B=1769 1 Solution

X=pavel I, A=1796, B=1801

X=aleksandr I, A=1801, B=1825

X=nikolay I, A=1825, B=1855

X=aleksandr II, A=1855, B=1881

X=aleksandr III, A=1881, B=1894

X=nikolay II, A=1894, B=1917

8 Solutions

4.

:

domains

name=symbol

predicates

star(name)

planet(name)

revolve(name,name)

satellite(name,name).

goal

satellite(X,""),

write(X," ."),

nl.

clauses

star("").

planet("").

planet("").

revolve("","").

revolve("","").

revolve("","").

revolve("","").

revolve("","").

satellite(X,Y):-planet(Y),revolve(X,Y).

: .

: , . .

domains

name=symbol

predicates

men(name)

mama(name)

sons(name,name)

doughter(name,name)

deda(name,name)

brother(name,name).

goal

doughter(Z,Y),

write(Z,Y),

nl.

clauses

men(Vaciliy).

men(Michail).

men(Maxim).

mama(Olga).

sons(Michail,Olga).

sons(Maxim,Olga).

doughter(Olga,Vasiliy).

deda(X,Y) :--men(X),men(Y),sons(X,Y),doughter(Z,Y).

brother(X,Y) :--men(X),men(Y), sons(X,Z),sons(Y,Z),X<>Y.

: Olga, Vaciliy

5.

: 0 1.

goal

random(X),

Z=X,

write(" 0 1 : ",Z),

nl.

: 0,64823988962

0 10 10 35.

goal

random(10,X),

Z=X,

write(" 0 10 : ",Z),

nl.

: 5

goal

random(25,X),

Z=X++10,

write(" 10 35:,Z),

nl.

: 21

6.


goal

write("X = "),

readint(X),

nl,

write("K = "),

readreal(K),

nl,

Z=exp(sin(X))+sqrt(K+X*X),

write("Z = ",Z).

: X=5, K=16, Z=6,7864292326

X = (2 + 5) * 3,4, .. 2, 5, 3.4 .

goal

write("X = "),

readint(X),

nl,

write("K = "),

readint(K),

nl,

write(N= ),

readreal(N),

nl

Z=(X+K)*N,

write("Z = ",Z).

: 23,8

7.

, .

predicates

operation(symbol,real,real)

clauses

operation("+",X,Y):-Z=X+Y,

write(X,"+",Y,"=",Z),

nl.

operation("-",X,Y):-Z=X-Y,

write(X,"-",Y,"=",Z),

nl.

operation("*",X,Y):-Z=X*Y,

write(X,"*",Y,"=",Z),

nl.

operation("/",X,Y):-Z=X/Y,

write(X,"/",Y,"=",Z),

nl.

: Z=8+2 Z=8-2 Z=8*2 Z=8/2

Z=10 Z=6 Z=16 Z=4

1 Solution 1 Solution 1 Solution 1 Solution

2.

predicates

operation(symbol,real,real)

Goal

write(Vvedite chisla),

nl,

readreal(X),

nl,

readreal(Y),

nl,

operation(+,X,Y),

operation(-,X,Y),

operation(*,X,Y),

operation(/ ,X,Y).

clauses

operation("+",X,Y):-Z=X+Y,

write(X,"+",Y,"=",Z),

nl.

operation("-",X,Y):-Z=X-Y,

write(X,"-",Y,"=",Z),

nl.

operation("*",X,Y):-Z=X*Y,

write(X,"*",Y,"=",Z),

nl.

operation("/",X,Y):-Z=X/Y,

write(X,"/",Y,"=",Z),

nl.

: Vvedite chisla

2

4

2+4=6

2-4=-2

2*4=8

2/4=0,5

9.

4 , fail, .

domains

name=symbol

predicates

men(name)

mama(name)

sons(name,name)

doughter(name,name)

deda(name,name)

brother(name,name).

goal

deda(X,Vaciliy),

write(X,Vaciliy),

nl.

clauses

men(Vaciliy).

men(Michail).

men(Maxim).

mama(Olga).

sons(Michail,Olga).

sons(Maxim,Olga).

doughter(Olga,Vasiliy).

deda(X,Y) :--men(X),men(Y),sons(X,Y),doughter(Z,Y),nl,

write( ,X),nl, fail.

brother(X,Y) :--men(X),men(Y), sons(X,Z),sons(Y,Z),X<>Y.

: Michail

Maxim

10.

:

1 + 2 + 3 + ... + 9 + 10

2 + 4 + 6 + ... + 14 + 16

10 + 9 + 8 + ... + 2 + 1

1 + 3 + 5 + ... + 13 + 15

domains

number,sum=integer

predicates

sum(number,sum)

goal

write( : ),

sum(1,sum), write(sum).

clauses

sum(11,0).

sum(Number, Sum) :--

New_number=Number+1,

sum(New_number,Partial_sum),

Sum=Number+Partial_sum.

: : 55

domains

number,sum=integer

predicates

sum(number,sum)

goal

write( : ),

sum(2,sum), write(sum).

clauses

sum(18,0).

sum(Number, Sum) :--

New_number=Number+2,

sum(New_number,Partial_sum),

Sum=Number+Partial_sum.

: : 72

domains

number,sum=integer

predicates

sum(number,sum)

goal

write( : ),

sum(9,sum), write(sum).

clauses

sum(0,11).

sum(Number, Sum) :--

New_number=Number-1,

sum(New_number,Partial_sum),

Sum=Number+Partial_sum

: : 55

domains

number,sum=integer

predicates

sum(number,sum)

goal

write( : ),

sum(1,sum), write(sum).

clauses

sum(17,0).

sum(Number, Sum) :--

New_number=Number+2,

sum(New_number,Partial_sum),

Sum=Number+Partial_sum.

: : 64

12.

, . .

domains

town_list=town*

town=symbol

predicates

towns(town_list)

goal

towns([A,B,C,D,E]),

write(A,,,B,,,C,,,D,,,E).

clauses

towns([ Kazan,Nignekamsk,Elabuga,Bugulma,Almetevsk ]).

: Kazan, Nignekamsk, Elabuga, Bugulma, Almetevsk


Prolog 1. , . predicates hello. goal hello. clauses hello:- makewindow(1,7,7," ",4,

 

 

 

! , , , .
. , :