logic – The problem about pigs

Question:

There is a task:

there are four pigs (a, b, c, d)

they are from (australia, germany, france, ireland)

feed (grass, vegetables, eggs, chestnuts)

pig "b" from germany. Australian pig eats vegetables. Irish girl doesn't like eggs. the pig "in" eats chestnuts, and the pig "a" is not from france and eats grass.

Where is the pig from and what does it eat?

Help with the solution, I'm dulling something.

Answer:

Approach how to solve similar logical problems using the prologue:

First you need to determine the facts that are known:

имя(а).
имя(б).
имя(в).
имя(г).

корм(трава).
корм(овощи).
корм(яйца).
корм(каштаны).

страна(австралия).
страна(германия).
страна(франция).
страна(ирландия).

Next is the function that will decide:

solve(Свиньи):-

We collect the necessary list, which we will return later

Свиньи = [свинья(А, АСтрана, АКорм), 
          свинья(Б, БСтрана, БКорм), 
          свинья(В, ВСтрана, ВКорм),
          свинья(Г, ГСтрана, ГКорм)],

We indicate that the specified values ​​must be unique

имя(А), имя(Б), имя(В),имя(Г), unique([А,Б,В,Г]),
корм(АКорм), корм(БКорм),корм(ВКорм),корм(ГКорм), unique([АКорм, БКорм, ВКорм,ГКорм]),
страна(АСтрана), страна(БСтрана),страна(ВСтрана),страна(ГСтрана), unique([АСтрана, БСтрана, ВСтрана,ГСтрана]),

Next, we indicate the specific conditions from the problem

% свинья "б" из германии. 
member(свинья(б, германия, _), Свиньи),

% австралийская свинья ест овощи. 
member(свинья(_, австралия, овощи), Свиньи), 

% свинья "в" ест каштаны,
member(свинья(в, _, каштаны), Свиньи), 

% а свинья "a" не из франции и ест траву.
member(свинья(а,НеЛюбитФранцию, трава), Свиньи), not(НеЛюбитФранцию=франция),

% ирландская не любит яйца. 
member(свинья(_, ирландия, НеЛюбитЯйца), Свиньи), not(НеЛюбитЯйца=яйца).

We add a trigger function that will call the decisive

start(Solve):-
  solve(Solve), Solve = [свинья(а, _, _), свинья(б, _, _), свинья(в, _, _), свинья(г,_,_)].

When you start start(Решение). we get the answer:

Solution = [pig (a, ireland, grass), pig (b, germany, eggs), pig (c, france, chestnuts), pig (g, australia, vegetables)]
false

Scroll to Top