1

What I have to do is to unify the possible options and solve the problem with these sentences

  1. The Spaniard lives next to the red house.
  2. The Norwegian lives in the blue house.
  3. An Italian lives in the second house.

This is my attempt but I am getting an error, could someone please help me.

neighborhood(N):-
  length(V,3),
  next(house(_,spaniard), house(red,_), V),
  member(house(blue,norway), V),
  V = [_|house(_,italian)].
false
  • 10,264
  • 13
  • 101
  • 209
Miguel JV
  • 67
  • 5

1 Answers1

1

You may write a procedure that enforces each of your rules, and then let prolog find the possible ordering of houses that fulfill all those rules:

neiborhood(Houses):-
 Houses=[House1, Italy, House3], % these are the houses, after rule 3
 Italy=house(_ItalyColor, italy),
 Spain=house(_SpainColor, spain),
 
 % rule 2:
 Norway=house(blue, norway),
 member(House1-House3, [Spain-Norway, Norway-Spain]),

 % rule 1:
 append(_, [HouseA, HouseB|_], Houses),
 (HouseA-HouseB=Spain-house(red, _) ; HouseB-HouseA=Spain-house(red, _)).

In this code I assumed when you said that the Spaniard lives next to the red house that it may live "to the left" or "to the right" of that house. Also note that you only mention 2 house colors, so the third one gets unassigned color. Maybe you are missing another rule, possible which is the missing color.

Sample run:

?- neiborhood(Houses).
Houses = [house(_163550, spain), house(red, italy), house(blue, norway)] ;
Houses = [house(blue, norway), house(red, italy), house(_163550, spain)] ;
false.

In both solutions, the Spain house does not have any color assigned.

gusbro
  • 22,357
  • 35
  • 46
  • There are no missing rules, in fact the solution is correct, the goal was, as you said, to find all possible solutions. I have only one doubt, how does the `-` operator work? I have not seen it in class. @gusbro. – Miguel JV Mar 31 '22 at 14:30
  • 1
    `-` in `House1-House3` is not an operator, its just the principal functor of `-(House1, House3)`. The use of `member(House1-House3, [Spain-Norway, Norway-Spain])` is to unify upon backtracking the two terms, first `House1-House3` with `Spain-Norway` and on backtracking `House1-House3` with `Norway-Spain`. So basically it is used to get both possible orderings for Spain and Norway. – gusbro Mar 31 '22 at 14:48