程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我在比较列表序言程序中遗漏了什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决我在比较列表序言程序中遗漏了什么??

开发过程中遇到我在比较列表序言程序中遗漏了什么?的问题如何解决?下面主要结合日常开发的经验,给出你关于我在比较列表序言程序中遗漏了什么?的解决方法建议,希望对你解决我在比较列表序言程序中遗漏了什么?有所启发或帮助;
isgreater([H1|T1],[H2|T2],D):-
  H1 > H2,isgreater(T1,T2,['Yes'|D]).

isgreater([H1|T1],D):-
  H1 =< H2,['No'|D]).

isgreater([],[],D).

理想的输出应该是

{No,No,Yes}

这是痕迹

 Call: (10) isgreater([1,2,3,4,5],[4,4],_49600) ? creep
   Call: (11) 1>4 ? creep
   Fail: (11) 1>4 ? creep
   Redo: (10) isgreater([1,_49600) ? creep
   Call: (11) 1=<4 ? creep
   Exit: (11) 1=<4 ? creep
 * Call: (11) isgreater([2,('No'| _49600)) ? creep
   Call: (12) 2>4 ? creep
   Fail: (12) 2>4 ? creep
 * Redo: (11) isgreater([2,('No'| _49600)) ? creep
   Call: (12) 2=<4 ? creep
   Exit: (12) 2=<4 ? creep
 * Call: (12) isgreater([3,('No'| 'No'| _49600)) ? creep
   Call: (13) 3>4 ? creep
   Fail: (13) 3>4 ? creep
 * Redo: (12) isgreater([3,('No'| 'No'| _49600)) ? creep
   Call: (13) 3=<4 ? creep
   Exit: (13) 3=<4 ? creep
 * Call: (13) isgreater([4,('No'| 'No'| 'No'| _49600)) ? creep
   Call: (14) 4>4 ? creep
   Fail: (14) 4>4 ? creep
 * Redo: (13) isgreater([4,('No'| 'No'| 'No'| _49600)) ? creep
   Call: (14) 4=<4 ? creep
   Exit: (14) 4=<4 ? creep
 * Call: (14) isgreater([5],[4],('No'| 'No'| 'No'| 'No'| _49600)) ? creep
   Call: (15) 5>4 ? creep
   Exit: (15) 5>4 ? creep
 * Call: (15) isgreater([],('Yes'| 'No'| 'No'| 'No'| 'No'| _49600)) ? creep
 * Exit: (15) isgreater([],('Yes'| 'No'| 'No'| 'No'| 'No'| _49600)) ? creep
 * Exit: (14) isgreater([5],('No'| 'No'| 'No'| 'No'| _49600)) ? creep
 * Exit: (13) isgreater([4,('No'| 'No'| 'No'| _49600)) ? creep
 * Exit: (12) isgreater([3,('No'| 'No'| _49600)) ? creep
 * Exit: (11) isgreater([2,('No'| _49600)) ? creep
   Exit: (10) isgreater([1,_49600) ? creep

我在这里错过了一些要点,但我就是想不通。

  • 程序只能返回真值。
  • 我只能在列表的前面附加一个元素。
  • 在 isgreater([],D) 的情况下,我无法得到 D。

解决方法

您可以通过以下方式解决问题:

isgreater([H1|T1],[H2|T2],['Yes'|D]) :- % solution is ['Yes'|D] if
  H1 > H2,% H1 is greater than H2 and
  isgreater(T1,T2,D).                   % D is the solution for the small
                                          % instance of the problem

isgreater([H1|T1],['No'|D]):-   % analogous to the last case
  H1 =< H2,isgreater(T1,D).

isgreater([],[],[]).                   % solution is empty if both lists are empty!

运行示例:

?- isgreater([1,2,3,4,5],[4,4],A).
A = ['No','No','Yes'] ;
false.

?- isgreater([1,1,5,2],'Yes','Yes'] ;
false.

?- isgreater([],A).
A = [].

大佬总结

以上是大佬教程为你收集整理的我在比较列表序言程序中遗漏了什么?全部内容,希望文章能够帮你解决我在比较列表序言程序中遗漏了什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: