Haskell中的单词和值的元组中的较低值 [英] The lower value from a tuple of words and values in haskell

查看:48
本文介绍了Haskell中的单词和值的元组中的较低值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数选择具有较低值的元组,并且该元组由一个单词和一个值组成.

I'm trying to write a function that chooses the tuple with the lower value and the tuple is formed by a word and a value.

例如,对于像这样的元组列表:[("CHURCH",262),("PENCIL",311),("FLOUR",175),("FOREST",405),("CLASS,105)],该函数将返回(" CLASS,105)

For example, for a list of tuples like this one: [("CHURCH",262),("PENCIL",311),("FLOUR",175),("FOREST",405),("CLASS",105)], the function would return ("CLASS",105)

有人可以帮我吗? :)

Can someone help me out? :)

非常感谢您!

推荐答案

尝试一下:

foldl1 (\acc x -> if snd x < snd acc then x else acc) <your list>

通过将列表的当前元素与下一个元素进行比较来折叠列表.如果下一个元素的第二个值小于当前元素的值,则它是累加器的新值.否则,累加器保持不变.重复此过程,直到遍历了整个列表.

You fold your list by comparing the current element of the list to the next one. If the second value of the next element if smaller than the value of the current element, it is the new value for the accumulator. Else the accumulator stays the same. This process is repeated until the entire list has been traversed.

这篇关于Haskell中的单词和值的元组中的较低值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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