斑马拼图——谁住在哪一层?

Zebra puzzle - who lives on which floor?

我有一个 zebra/Einstein 类型的 Prolog 谜题,与此处描述的非常相似:

.

谜语是:

There are two buildings, each one has tree apartments (one apartment on each floor): one apartment of 3 rooms, one of 4 rooms and one of 5 rooms.

Dana, Joni and Noah live in building 1. Ron, Gale and Aron live in building 2.

Joni's apartment is higher then Dana's and Noah's. It means he lives on the third floor of building 1. Noah and Gale live on the same floor (in different buildings). Ron has one more room than Aron. Ron lives one floor above Gale. The highest apartment in building 2 is the 5 rooms apartment.

我需要找到每个人都住在哪一层。

这是代码。

solve([dana,building1,F1,R1],
      [noah,building1,F2,R2],
      [joni,building1,F3,R3],
      [gale,building2,F4,R4],
      [ron, building2,F5,R5],
      [aron,building2,F6,R6]) :-
   assign([1,2,3],[F1,F2,F3]),
   assign([1,2,3],[F4,F5,F6]),
   assign([3,4,5],[R1,R2,R3]),
   assign([3,4,5],[R4,R5,R6]),
   F3 > F2,
   F3 > F1,
   F2 =:= F4,
   R5 =:= R6-1,
   F5 =:= F4+1,
   (  F4 =:= 3, R4 =:= 5
   ;  F5 =:= 3, R5 =:= 5
   ;  F6 =:= 3, R6 =:= 5
   ).

assign(_,[]).
assign(Digs,[D|Vars]):-
   del(D,Digs,Digs1),
   assign(Digs1,Vars).

del(X,L,L1) :-
   remove(X,L,L1).

remove([],X,[]) :- !. 
remove([X|T],X,L1) :- !, remove(T,X,L1).   
remove([H|T],X,[H|L1]) :- remove(T,X,L1).

我不明白以下解决方案应使用哪个查询?当我尝试从原始 post、

查询时
solve([dana,building1,F1,R1],[noah,building1,F2,R2],[joni,building1,F3,R3],
      [gale,building2,F4,R4],[ron, building2,F5,R5],[aron,building2,F6,R6]).

它说

Type error: `evaluable' expected, found `[]' (an empty_list)
In:
   [2] []>[]
   [1] solve([dana,building1|...],[noah,building1|...],[joni,building1|...],
             [gale,building2|...],[ron, building2|...],[aron,building2|...]) 
at  line 1

正确的查询是什么?

谢谢。

给定代码的查询是正确的。

代码包含错误。

del(X,L,L1) :-
   remove(X,L,L1).   % WRONG

您需要更改对 remove/3 的调用中参数的顺序,以对应它们在 remove/3 的定义中的使用。

您可以通过在 small 中测试 来发现它。只需在提示符下尝试查询 assign([1,2,3],Floors),您就会发现它是否按预期工作。

del(D,[1,2,3],Digs1),遵循assign/2定义,

assign([1,2,3],[D|Vars]):-
   <b>del(D,[1,2,3],Digs1)</b>,
   ..... .

remove(D,[1,2,3],Digs1),遵循del/3定义,

del(D,[1,2,3],Digs1) :-
   <b>remove(D,[1,2,3],Digs1)</b>.

查看定义

remove([],   <b>X</b>,[]    ) :- !. 
remove([X|T],<b>X</b>,L1    ) :- !, remove(T,X,L1).
remove([H|T],<b>X</b>,[H|L1]) :- remove(T,X,L1).</pre>

知道我们对 D 的预期含义是从 ​​[1,2,3] 中保留一个 数字

(其余故意留空)