Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

logic - Riddle with GNU Prolog, similar to Einstein Riddle

I am a complete beginner to programming and have to create and solve a riddle in Prolog using GNU Prolog, similar to the Einstein riddle, albeit less sophisticated. I have been trying to create a riddle for the items contained within the following table.

My code so far look like this, but I really do not entirely understand what I am doing wrong or right here. I can compile the code in GNU Prolog, but it will not solve the riddle:

middle(M,[_,M,_]).
right(A,B,[[_|A]|B]).
left(A,B,[A|[B|_]]).
run:-
   X = [_,_,_],
   middle([_,brown,_],X),   /* the brown guinea pig lives in the middle of the cage */
   member([brown,carrots,_],X), /* the brown guinea pig loves to eat carrots */
   member([_,salad,giggles],X), /* the salad eating guinea pig giggles */
   right([_,salad,_],[brown,_,_],X),    /* the salad eating guinea pig sits to the right of the brown guinea pig */
   left([black,_,_],[_,_,squeaks],X),   /* the black guinea pig sleeps to the left of the squeaking guinea pig */
   member([black,_,grumbles],X),    /* the black guinea pig grumbles */
   member([grey,_,giggles],X),  /* the grey guinea pig giggles*/
   write(X),nl, /* write out all fur colors */
   write('the '),write(N),write(' guinea pig loves to eat cucumbers'),nl. /* answer to the question */

I would greatly appreciate any help as I am quite unfamiliar with these things but have to figure out a solution for a class I am taking. Any tips would be of great help. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just be careful, and keep things uniform. Keep each thing in its own place. And, you had mangled some list codes which needed fixing. So, here you go.

middle(M, [_,M,_]).
right(A,B,X) :- left(B,A,X).
left(A,B,X) :- append(_, [A,B | _], X).

run :-
   X = [_,_,_],
   middle([_       ,brown,_      ],X),   /* the brown guinea pig - middle of the cage */
   member([_       ,brown,carrots],X),   /* the brown guinea pig loves to eat carrots */
   member([giggles ,_    ,salad  ],X),   /* the salad-eating guinea pig giggles */
   right( [_       ,_    ,salad  ],      /* the salad-eating guinea pig sits */
          [_       ,brown,_      ],X),   /*    to the right of the brown guinea pig */
   left(  [_       ,black,_      ],      /* the black guinea pig sleeps to the left */
          [squeaks ,_    ,_      ],X),   /*    of the squeaking guinea pig */
   member([grumbles,black,_      ],X),   /* the black guinea pig grumbles */
   member([giggles ,grey ,_      ],X),   /* the grey guinea pig giggles */
   member([_       ,EC ,cucumbers],X),   /* a guinea pig that loves to eat cucumbers */

   X = [[_,A,_],[_,B,_],[_,C,_]], write([A,B,C]), nl, /* write out all fur colors */
   write('the '), write(EC),
   write(' guinea pig loves to eat cucumbers'), nl. /* the answer to the question */

But, some might argue, the human programmer has infused too much of his understanding here (i.e. cheated somewhat) by making the mold with three places for the three attributes that he knows guinea pigs have in this universe.

This is not necessary though. Here's how we let the "computer" to figure that out all by itself, using kind of "extensible records" :

attr(A, [N-V | R]):- memberchk( N-X, A), X = V, attr(A, R).
attr(_, []).

color(A, B):- attr( A, [color-B]).

pigs( Pigs):-
  length( Pigs,N), 
  N rem 2 =:= 1, Middle is N div 2,    /* there _is_ a middle - list length is odd */
  nth0( Middle,Pigs,P1), attr( P1, [color-brown]), 
  member( P2, Pigs),     attr( P2, [color-brown, eats-carrots]),
  member( P3, Pigs),     attr( P3, [eats-salad, sound-giggles]),
  right( P4,P4b,Pigs),   attr( P4, [eats-salad]),   attr( P4b, [color-brown]),
  left(  P5,P5b,Pigs),   attr( P5, [color-black]),  attr( P5b, [sound-squeaks]),
  member( P6, Pigs),     attr( P6, [color-black, sound-grumbles]),
  member( P7, Pigs),     attr( P7, [color-grey,  sound-giggles]),
  member( P8, Pigs),     attr( P8, [eats-cucumbers, color-EatsCucumbers]),
  length( Furs, N),      maplist( color, Pigs, Furs),
  writeln( Furs),        writeln( EatsCucumbers),  nl, !.

Testing:

14 ?- time(( pigs(_P), maplist(writeln,_P), ! )).
[black,brown,grey]
black

[color-black, sound-grumbles, eats-cucumbers|_G1484]
[color-brown, eats-carrots,   sound-squeaks |_G1424]
[eats-salad,  sound-giggles,  color-grey    |_G1463]
/* % 287 inferences, 0.000 CPU in 0.030 seconds (0% CPU, Infinite Lips) */
true.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...