使用GNU Prolog进行Riddle,类似于Einstein Riddle



我是一个完全的编程初学者,必须使用GNU Prolog在Prolog中创建和解决一个谜题,类似于爱因斯坦谜题,尽管不那么复杂。我一直试图为下表中的项目制造一个谜语。

到目前为止,我的代码看起来是这样的,但我真的不完全理解我在这里做错了什么或做对了什么。我可以用GNU Prolog编译代码,但它不会解决这个谜题:

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 */

我非常感谢任何帮助,因为我对这些事情很不熟悉,但必须为我正在上的课程找到解决方案。任何提示都会有很大帮助。谢谢

只要小心,保持一致。把每样东西都放在自己的地方。而且,您已经损坏了一些需要修复的列表代码。所以,给你。

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 */

但是,有些人可能会争辩说,人类程序员在这里注入了太多的理解(即有些欺骗),因为他制作了一个带有三个位置的模具,用于他知道豚鼠在这个宇宙中拥有的三种属性。

但这并不是必须的。以下是我们如何让"计算机"使用的"可扩展记录" 自行解决这一切:

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, !.

测试:

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.

相关内容

  • 没有找到相关文章

最新更新