Prolog 获取列表的频率 [英] Prolog getting frequency of list

查看:46
本文介绍了Prolog 获取列表的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在进行序言作业.

我有一个结构cnt(letter,number).

我需要返回一个 cnt 的列表,其中每个 cnt 是每个字符出现的次数(假设每个项目已经排序以放置相同的项目一个接一个).

I need to return a list of cnt where each cnt is the number of times each character appears (assuming that each item has already been sorted to place the same item one after each other).

到目前为止我有这个:

cnt(letter,number).

freq([],[]).

freq([A|L],Y) :- grab(A,Init,_), freq(L,[Init|Y]).

grab 工作正常获取项目列表并返回第一个重复项的列表作为 Init

grab works correctly takes a list of items and returns the list of the first duplicates as Init

例如 grab([a,a,a,b,c], Init, Rest). 将返回 Init = [a,a,a].

假设我有一个列表 [a,a,a,b,b,b,c,c] 我需要 freq 返回 Y = [cnt(a,3),cnt(b,3), cnt(c,2)].

Assuming I have a list [a,a,a,b,b,b,c,c] i need freq to return Y = [cnt(a,3), cnt(b,3), cnt(c,2)].

我认为到目前为止我所知道的几乎是正确的,只是它返回了错误.

I think what I have so far is near correct, except that it returns false.

有没有办法通过一步一步看看它正在做什么才能到达那里?或者任何人都可以看到任何明显的问题.

Is there anyway to step through to see what it is doing to get there? Or can anyone see any obvious problems.

推荐答案

让我们从你的定义开始,它已经非常接近你想要的了.

Let's start with your definition, which is already very close to what you want.

freq([],[]).
freq([A|L],Y) :- grab(A,Init,_), freq(L,[Init|Y]).

freq/2 在这里定义了列表中的每个元素.为了看到这一点,我将查看您定义的以下部分:

freq/2 defines here something for each element of the list. To see this, I will look at the following part of your definition:

freq([],_).
freq([A|L],_) :- ..., freq(L,_).

这是你想要的吗?您说该列表仅连续包含相同的元素.所以如果我们有 [a,a] 你确实希望这个 freq/2 被应用一次,而不是两次.

Is this what you want? You said that the list contains the same elements consecutively only. So if we have [a,a] you do want this freq/2 to be applied once, not twice.

另一个问题是这个.再说一次,我只看你程序的一部分:

The other problem is this. Again, I am looking only at a part of your program:

freq(_,[]).
freq(_,Y) :- ..., freq(_,[Init|Y]).

所以你在这里有一个目标 freq(_,[Init|Y]),它在第二个参数中有一个 至少 一个元素的列表.您是否在定义中看到任何适用于此类列表的条款?事实 freq(_,[]). 从不适用,所以剩下的唯一规则就是出现这个目标的规则.简而言之,目标 freq(_,[Init|Y]) 永远不会成功.不管InitY是什么.

So you have here a goal freq(_,[Init|Y]) which has a list of at least one element in the second argument. Do you see any clause in your definition which applies to such lists? The fact freq(_,[]). never applies, so the only rule left is the very rule where this goal appears in. In short, a goal freq(_,[Init|Y]) will never succeed. No matter what Init and Y are.

这是您更正后的版本,几乎就是您想要的:

Here is now your corrected version, which is almost what you want:

freq([],[]).
freq([A|L],[As|Y]) :-
   grab([A|L],As,K),
   freq(K,Y).

让我们看看:

?- freq([a,a,a,b,b,b,c,c],Ys).
Ys = [[a,a,a],[b,b],[c,c]].

因此,我们需要一个结构cnt(a,3),其中包含列表的字符和长度,而不是那些列表元素.

So instead of those elements that are lists, we need a structure cnt(a,3) with the character and the length of the list.

freq([],[]).
freq([A|L],[cnt(A,N)|Y]) :-
   grab([A|L],As,K),
   length(As, N),
   freq(K,Y).

这篇关于Prolog 获取列表的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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