使用重复对序言中的事实集合进行排序 [英] Using repeat to sort a collection of facts in prolog

查看:39
本文介绍了使用重复对序言中的事实集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组事实 set/2,其中第一个变量是集合的标识符,第二个变量是与标识符关联的值.
例如:

I have a set of facts set/2 where the first variable is the identifier for the set and the second is the value associated with the identifier.
For example:

set(a,2).
set(a,c).
set(a,1).
set(a,a).
set(a,3).
set(a,b).

我需要构造一个谓词 ordering/2(使用重复运算符),它将按照字典顺序输出特定集合的值.例如

I need to construct a predicate ordering/2 (using the repeat operator) which will output the values of a specific set in their lexicographic order. For example

?- ordering(a,Output).

会导致

[1,2,3,a,b,c].

到目前为止我所做的是这段代码:

What I have made thus far is this code:

ordering(Input,Output):-    
    findall(X,set(Input,X),List),
    repeat,
    doSort(List)
    sort(List, OrderedList),
    Output = OrderedList. 

这里的想法是谓词将找到与特定输入关联的集合的所有值,并将 List 变量与它们统一.现在我们有了未排序的列表.这是我不完全确定的部分.谓词应该继续在列表上使用某种特定的 doSort,然后使用 sort/2 检查列表,如果它是按字典顺序排列的,则将其与输出统一.

The idea here is that the predicate will find all values of the set associated with the specific Input and unify the List variable with them. Now we have the unsorted List. Here comes the part I'm not completely sure on. The predicate is supposed to keep using some sort of specific doSort on the List, then check the List with sort/2 and if it's lexicographically ordered, unify it with the Output.

谁能澄清我是否在正确的道路上,如果是,那么应该如何实施 doSort?

Can anyone clarify if I'm on the correct path and if yes then how should the doSort be implemented?

推荐答案

好吧,我尝试使用 @Daniel lyon 的帮助来解决这个问题:

Alright, I tried a sort of answer for this using @Daniel lyon's help:

ordering(Input,Output):-    
   findall(X,set(Input,X),List),
    repeat,
    permutation(List,PermutationList),
    sort(PermutationList, SortedList),
    Output= SortedList , !. 

大体思路是一样的,对于repeat循环,谓词将List与PermutationList统一,尝试所有变体并用sort/2检查它们的正确性,直到达到正确的排列,将其与SortedList统一,之后,它将使用 SortedList 统一输出.剪辑在那里,所以我只会得到一次输出.

The general idea is the same, for the repeat cycle, the predicate will unify the List with PermutationList, try all variants of it and check for their correctness with sort/2 until it achieves the correct permutation, unifying it with SortedList, after that it will unify the Output with SortedList. The cut is there so I will only get the Output once.

这篇关于使用重复对序言中的事实集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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