其间0变量和一些序言的多个值 [英] Multiple values of a variable inbetween 0 and a number prolog

查看:149
本文介绍了其间0变量和一些序言的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在试图教自己序言和我想我来一直很好。不过,我那种停留在这一个方法,我试图做。

So I've been trying to teach myself prolog and I think I'm coming along nicely. However, I'm sort of stuck at this one method I'm trying to make.

吨(N,A)等于0和N-1的整数值,升序排序产生的。

toN(N,A) A is equal to the integer values between 0 and N-1, generated in ascending order.

所以 吨(5,A)是

A = 0;
A = 1;
A = 2;
A = 3;
A = 4.

我还是新来的Prolog所以我不完全知道如何与多个值做到这一点。我有这样的事情:

I'm still new to prolog so I'm not exactly sure how to do this with multiple values. I had something like this:

toN(N,A) :- 0 < N, Nx is N-1, toN(Nx,A).
toN(N,A) :- 0 =< N, Nx is N-1, A = Nx.

然而,这只是返回false。没有其他的。这似乎完全没有给我

However this just returns false. Nothing else. It seems perfectly fine to me

推荐答案

检查Prolog的实现,它使用的是支持的 clpfd

Check if the Prolog implementation that you are using supports clpfd!

:- use_module(library(clpfd)).

的实施吨/ 2 得到声明和超简洁的:

The implementation of toN/2 gets declarative and super-concise:

toN(N,A) :- 
   A #>= 0, 
   A #<  N, 
   labeling([up],[A]).

您会发现在clpfd手册更多标签选项: SWI-Prolog的clpfd SICStus Prolog的clpfd

You'll find more labeling options in the clpfd manual: SWI-Prolog clpfd, SICStus Prolog clpfd.

这篇关于其间0变量和一些序言的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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