本文介绍了Prolog - 生成适合给定范围的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用如下谓词:
range(X,0,5)
range(X,4,200)
range(X,-1000000,1000000)
dom_range(X,-1000000,1000000)
含义:
range(X,0,5) :- member(X,[0,1,2,3,4,5]).
range(X,4,200) :- member(X,[4,5,6...198,199,200]).
range(X,-1000000,1000000) :- member(X,[-1000000,...,1000000]).
dom_range(X,-1000000,1000000) :- domain(X, [-1000000,...,1000000]).
如何在 Prolog 中很好地编写代码(考虑解决方案性能 - 递归深度等)?
How to code it in Prolog nicely (taking solution performance into account - depth of recursion etc) ?
解决方案有望在 GNU-Prolog 上运行.
Solution is expected to run on GNU-Prolog.
附:受这个问题启发的问题.
推荐答案
SWI-Prolog 有谓词 介于/3.因此您可以将其称为 between(0,5,X) 以获得上面显示的结果.这个谓词看起来像是在 C 中实现的.
SWI-Prolog has the predicate between/3. so you would call it like between(0,5,X) to get the results you showed above. This predicate looks like this is implemented in C though.
如果我们必须用纯 prolog 编写它(速度和空间不是一个因素),你可以试试下面这个.
If we have to write it in pure prolog (and speed&space is not a factor), you could try this following.
range(Low, Low, High).
range(Out,Low,High) :- NewLow is Low+1, range(Out, NewLow, High).
这篇关于Prolog - 生成适合给定范围的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!