序言列表中的最小值 [英] minimum in list of lists in prolog

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

问题描述

你好,我有一个这样的列表:

hello i have a list like this:

[[3,[a,b,c,d]],[2,[a,b,d]],[5,[d,e,f]]]

列表列表...我想在内部列表中找到最小数量在这种情况下,我想返回D = 2和L = [a,b,d]

list of lists... i want to find the minimum number on inner list in this case i want to return D=2 and L=[a,b,d]

我尝试了以下代码:

minway([[N|L]],N,L).
minway([[M|L1]|L2],D,_):- M<D, minway(L2,M,L1).
minway([[M|_]|L2],D,L):- M>=D, minway(L2,D,L).

但我有错误:

</2: Arguments are not sufficiently instantiated
   Exception: (8) minway([[3,[a,b,c,d]],[2,[a,b,d]],[5,[d,e,f]]], _G7777, _G7778) ? 
   creep

对于此连续句子:

minway([[3,[a,b,c,d]],[2,[a,b,d]],[5,[d,e,f]]],D,L).

结果必须为:

D=2.
L=[a,b,d].

我的问题在哪里?以及如何解决?

where my problem? and how to fix it?

很多tnx

推荐答案

首先,切换到更好的数据表示形式:代替 [Key,Value] ,使用 Key-Value

First, switch to a better data representation: Instead of [Key,Value], use Key-Value!

然后,根据以下内容定义 minway_/3 iwhen/2 ground/1 keysort/2 ,然后 member/2 ,就像这样:

Then, define minway_/3 based on iwhen/2, ground/1, keysort/2, and member/2, like so:

minway_(Lss, N, Ls) :-
   iwhen(ground(Lss), (keysort(Lss,Ess), Ess = [N-_|_], member(N-Ls, Ess))).

使用SICStus Prolog 4.5.0进行示例查询:

Sample query using SICStus Prolog 4.5.0:

| ?- minway_([3-[a,b,c,d],2-[a,b,d],5-[d,e,f],2-[x,t,y]], N, Ls).
N = 2, Ls = [a,b,d] ? ;
N = 2, Ls = [x,t,y] ? ;
no

这篇关于序言列表中的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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