我应该如何在序言中设计此谓词? [英] how should i design this predicate in prolog?

查看:52
本文介绍了我应该如何在序言中设计此谓词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写一个称为stepup(L, Z, X)的谓词,其中L是一个列表,而ZX是整数.如果可以使用列表中用户给出的合法步骤将Z踏入X,则应返回true.

I have to write a predicate called stepup(L, Z, X), where L is a list and Z and X are the integers. It should return true if the Z can be stepped into X using legal steps given by the user in the list.

例如

 stepup([7, 12, 19], 6, 32) 

应该返回true,因为6 + 7 + 7 + 12 = 32 起始编号应始终为Z(此处为6),并且规则应仅使用列表中的步骤.该规则适用于所有尺寸,(Z, X)始终为正.

should return true since 6 + 7 + 7 + 12 = 32 starting number should always be Z (here 6) and the rule should only use steps from the list. The rule should work for all sizes, (Z, X) are always positive.

我从这里开始

step([V|S],A,D):- 
   sum is A+V,
   (sum=A -> write('true') 
   ; step(S,sum,D).

但不确定如何进行

推荐答案

也许有帮助:

stepup(_,I,I).
stepup(Steps,I0,I) :-
   I0 < I,
   member(Step,Steps),
   I1 is I0 + Step,
   stepup(Steps,I1,I).

让我们使用它!

?- stepup([7,12,19],6,31).
false.

?- stepup([7,12,19],6,32).
true ;
true ;
true ;
true ;
true ;
false.

请注意,第二个查询中的多余答案是由于使用步骤[7,12,19]6升级到32的方式不同.

Note that the redundant answers in the second query are due to the different ways to step up from 6 to 32 using steps [7,12,19].

这篇关于我应该如何在序言中设计此谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆