序言列表中的最小值

编程入门 行业动态 更新时间:2024-10-28 08:31:27
本文介绍了序言列表中的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

序言列表中的最小值

本文发布于:2023-10-30 11:17:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序言   最小值   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!