Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
21
Task/Zebra-puzzle/Prolog/zebra-puzzle-1.pro
Normal file
21
Task/Zebra-puzzle/Prolog/zebra-puzzle-1.pro
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
select([A|As],S):- select(A,S,S1),select(As,S1).
|
||||
select([],_).
|
||||
|
||||
next_to(A,B,C):- left_of(A,B,C) ; left_of(B,A,C).
|
||||
left_of(A,B,C):- append(_,[A,B|_],C).
|
||||
|
||||
zebra(Owns, HS):- % color,nation,pet,drink,smokes
|
||||
HS = [h(_,norwegian,_,_,_),_, h(_,_,_,milk,_),_,_],
|
||||
select( [h(red,englishman,_,_,_),h(_,swede,dog,_,_),
|
||||
h(_,dane,_,tea,_), h(_,german,_,_,prince)], HS),
|
||||
select( [h(_,_,birds,_,pallmall),h(yellow,_,_,_,dunhill),
|
||||
h(_,_,_,beer,bluemaster)], HS),
|
||||
left_of( h(green,_,_,coffee,_), h(white,_,_,_,_), HS),
|
||||
next_to( h(_,_,_,_,dunhill), h(_,_,horse,_,_), HS),
|
||||
next_to( h(_,_,_,_,blend), h(_,_,cats, _,_), HS),
|
||||
next_to( h(_,_,_,_,blend), h(_,_,_,water,_), HS),
|
||||
next_to( h(_,norwegian,_,_,_), h(blue,_,_,_,_), HS),
|
||||
member( h(_,Owns,zebra,_,_), HS).
|
||||
|
||||
:- ?- time(( zebra(Who, HS), maplist(writeln,HS), nl, write(Who), nl, nl, fail
|
||||
; write('No more solutions.') )).
|
||||
30
Task/Zebra-puzzle/Prolog/zebra-puzzle-2.pro
Normal file
30
Task/Zebra-puzzle/Prolog/zebra-puzzle-2.pro
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
% populate domain by selecting from it
|
||||
nation(H,V):- memberchk( nation(X), H), X=V. % select the "nation" attribute
|
||||
owns( H,V):- memberchk( owns( X), H), X=V. % ...
|
||||
smoke( H,V):- memberchk( smoke( X), H), X=V.
|
||||
color( H,V):- memberchk( color( X), H), X=V.
|
||||
drink( H,V):- memberchk( drink( X), H), X=V.
|
||||
|
||||
to_the_left(A,B,HS):- append(_,[A,B|_],HS).
|
||||
next_to(A,B,HS):- to_the_left(A,B,HS) ; to_the_left(B,A,HS).
|
||||
middle(A, [_,_,A,_,_]).
|
||||
first(A, [A|_]).
|
||||
|
||||
zebra(Zebra,Houses):-
|
||||
length(Houses,5),
|
||||
member(H2, Houses), nation(H2, englishman), color( H2, red),
|
||||
member(H3, Houses), nation(H3, swede), owns( H3, dog),
|
||||
member(H4, Houses), nation(H4, dane), drink( H4, tea),
|
||||
to_the_left(H5,H5b,Houses), color(H5, green), color(H5b, white),
|
||||
member(H6, Houses), drink( H6, coffee), color( H6, green),
|
||||
member(H7, Houses), smoke( H7, 'Pall Mall'), owns( H7, birds),
|
||||
member(H8, Houses), color( H8, yellow), smoke( H8, 'Dunhill'),
|
||||
middle(H9, Houses), drink( H9, milk),
|
||||
first(H10, Houses), nation(H10, norwegian),
|
||||
next_to(H11,H11b,Houses), smoke( H11, 'Blend'), owns( H11b, cats),
|
||||
next_to(H12,H12b,Houses), owns( H12, horse), smoke(H12b, 'Dunhill'),
|
||||
member(H13, Houses), drink( H13, beer), smoke( H13, 'Blue Master'),
|
||||
member(H14, Houses), nation(H14, german), smoke( H14, 'Prince'),
|
||||
next_to(H15,H15b,Houses), nation(H15, norwegian), color(H15b, blue),
|
||||
next_to(H16,H16b,Houses), drink( H16, water), smoke(H16b, 'Blend'),
|
||||
member(Zebra,Houses), owns(Zebra, zebra).
|
||||
12
Task/Zebra-puzzle/Prolog/zebra-puzzle-3.pro
Normal file
12
Task/Zebra-puzzle/Prolog/zebra-puzzle-3.pro
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
?- time(( zebra(Z,HS), ( maplist(length,HS,_) -> maplist(sort,HS,S),
|
||||
maplist(writeln,S),nation(Z,R),nl,writeln(R) ), false ; true)).
|
||||
|
||||
[color(yellow),drink(water), nation(norwegian), owns(cats), smoke(Dunhill) ]
|
||||
[color(blue), drink(tea), nation(dane), owns(horse), smoke(Blend) ]
|
||||
[color(red), drink(milk), nation(englishman),owns(birds), smoke(Pall Mall) ]
|
||||
[color(green), drink(coffee),nation(german), owns(zebra), smoke(Prince) ]
|
||||
[color(white), drink(beer), nation(swede), owns(dog), smoke(Blue Master)]
|
||||
|
||||
german
|
||||
% 138,899 inferences, 0.060 CPU in 0.110 seconds (55% CPU, 2311655 Lips)
|
||||
true.
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
select([A|As],S):- select(A,S,S1),select(As,S1).
|
||||
select([],_).
|
||||
|
||||
next_to(A,B,C):- left_of(A,B,C) ; left_of(B,A,C).
|
||||
left_of(A,B,C):- append(_,[A,B|_],C).
|
||||
|
||||
zebra(Owns, HS):- % color,nation,pet,drink,smokes
|
||||
HS = [h(_,norwegian,_,_,_),_,h(_,_,_,milk,_),_,_],
|
||||
select( [h(red,englishman,_,_,_),h(_,swede,dog,_,_),h(_,dane,_,tea,_),
|
||||
h(_,german,_,_,prince)], HS),
|
||||
select( [h(_,_,birds,_,pallmall),h(yellow,_,_,_,dunhill),
|
||||
h(_,_,_,beer,bluemaster)], HS),
|
||||
left_of( h(green,_,_,coffee,_),h(white,_,_,_,_), HS),
|
||||
next_to( h(_,_,_,_,dunhill), h(_,_,horse,_,_), HS),
|
||||
next_to( h(_,_,_,_,blend), h(_,_,cats, _,_), HS),
|
||||
next_to( h(_,_,_,_,blend), h(_,_,_,water,_), HS),
|
||||
next_to( h(_,norwegian,_,_,_), h(blue,_,_,_,_), HS),
|
||||
member( h(_,Owns,zebra,_,_), HS).
|
||||
|
||||
:- zebra(Who, HS), maplist(writeln,HS), nl, write(Who), nl, nl, fail
|
||||
;
|
||||
write('No more solutions.').
|
||||
Loading…
Add table
Add a link
Reference in a new issue