Prolog-师
我是Prolog的新手。从字面上看这个星期。我希望编写一个接受两个数字x,y作为输入的递归序言。它应该输出以筛选所有数字列表小于或等于x可以均匀排除的所有数字的列表。
在过去的几天里,我一直在研究这一点,并且很困惑。我首先尝试将y分为1,在屏幕上显示此内容,然后重复该过程。任何人都会有关于工作的技巧吗?一旦我得到了,我将尝试确保它们小于或等于X。
到目前为止,我有以下内容,这可能是完全错误的。任何帮助都将受到赞赏。如果格式不佳,则表示歉意。
divide(X, Y) :-
S is 1,
Z is Y / S,
S is S + 1,
write(Z),
divide(X, Y).
I am brand new to Prolog. Literally this week. I am looking to write a recursive Prolog predicate that accepts two numbers X, Y as input. It should output to screen a list of all numbers less than or equal to X that are evenly divisible by Y.
I have been looking into this over the last few days and am thouroghly confused. I have started by trying to divide Y into 1, displaying this on screen and then repeating the process. Would anyone have tips on gettingthis working. Once I get that, I will then be trying to ensure these are less than or equal to X.
So far I have the below, which may be completely wrong. Any help is appreciated. Apologies if the formatting comes off badly.
divide(X, Y) :-
S is 1,
Z is Y / S,
S is S + 1,
write(Z),
divide(X, Y).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
小于20的数字除以3启动3、6、9,... IE从Divisor 3开始,每次添加3个,直到您超越了截止。
例如
,在您的代码中,这行:
您是说“如果2是3”,那么Prolog会说“不是,案件关闭,停在此处”并结束您的程序。当您做S+1时,您需要为结果使用新变量。只有Prolog系统才能更改变量,您不能(简化)。
这是无限循环的形状:
x和y永远不会改变,因此每次都会发生相同的调用;如果它已经超越了S = S+1,那么它将永远转弯。
这种形状:
如果它可以使用您的尝试方式,则每次将其重置为1。
由于您需要某种情况来处理循环和某种情况以处理停止,并进行了一些测试,以通过限制和通过循环传递的东西,我希望单词作为可变名称,快速您的短代码变成了我的更长的代码即使它没有做更多的作用。
(我不是从X开始而分开的,因为那意味着X必须是Y的倍数。我认为您应该能够要求少于10的数字,这些数字可以分别为9,而无需交易, 10/9 = 1.11 ...)
The numbers less than 20 which divide by 3 start 3, 6, 9, ... i.e. start with the divisor 3 and count up adding 3 each time until you get past the cutoff.
e.g.
In your code, this line:
is you saying "keep going if 2 is 3" and Prolog will say "it's not, case closed, stop here" and end your program. When you do S+1 you need to use a new variable for the result. Only the Prolog system can change variables, you can't (simplification).
This is the shape of an infinite loop:
X and Y never change so it's the same call each time; if it got past that S=S+1 then it would go round and round forever.
This shape:
If it worked how you are trying, it would reset S back to 1 each time.
Since you need some case to handle looping and some case to handle stopping, and some test for passing the limit and something to pass S on through the loops, and I wanted words as variable names, quickly your short code turns into my much longer code even though it doesn't do much more.
(I didn't start from X and divide down because that would mean X had to be a multiple of Y. I thought you should be able to ask for the numbers less than 10 which are divisible by 9 and get 9 without having to deal with 10/9=1.11...)