Prolog-师

发布于 2025-02-04 13:11:44 字数 360 浏览 2 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2025-02-11 13:11:44

小于20的数字除以3启动3、6、9,... IE从Divisor 3开始,每次添加3个,直到您超越了截止。

例如

countup(X, Y, Counter) :-  % if the counter is below the limit,
    Counter =< X,          
    writeln(Counter),      % print it.
    
    Next is Counter + Y,   % count up by +Y, e.g. +3

    countup(X, Y, Next).   % loop.


countup(X, _, Counter) :-  % if the counter is past the limit,
    Counter > X.           % stop.


divide(X, Y) :-            % start counting from Y, e.g. 3
    countup(X, Y, Y).

,在您的代码中,这行:

S is S + 1

您是说“如果2是3”,那么Prolog会说“不是,案件关闭,停在此处”并结束您的程序。当您做S+1时,您需要为结果使用新变量。只有Prolog系统才能更改变量,您不能(简化)。


这是无限循环的形状:

divide(X, Y) :-
    ____,
    ____,
    divide(X, Y).

x和y永远不会改变,因此每次都会发生相同的调用;如果它已经超越了S = S+1,那么它将永远转弯。


这种形状:

divide(X, Y) :-
    S is 1,
    ____,
    divide(X, Y).

如果它可以使用您的尝试方式,则每次将其重置为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.

countup(X, Y, Counter) :-  % if the counter is below the limit,
    Counter =< X,          
    writeln(Counter),      % print it.
    
    Next is Counter + Y,   % count up by +Y, e.g. +3

    countup(X, Y, Next).   % loop.


countup(X, _, Counter) :-  % if the counter is past the limit,
    Counter > X.           % stop.


divide(X, Y) :-            % start counting from Y, e.g. 3
    countup(X, Y, Y).

In your code, this line:

S is S + 1

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:

divide(X, Y) :-
    ____,
    ____,
    divide(X, Y).

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:

divide(X, Y) :-
    S is 1,
    ____,
    divide(X, Y).

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...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文