如何找到列表中最长的单词? [英] How to find the longest word in list?
本文介绍了如何找到列表中最长的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法以功能性思维方式让我的头脑以简单的方式解决这个问题,这种方式也适用于很长的列表。如果您有以下列表:
I can't get my mind in a functional mindset to solve this problem in a simple way that could also work for very long lists. If you have a list like:
["one", "two", "three", "four", "five"]
我可以简单地说出最长单词的长度:
I can tell what the length of the longest word is pretty simply with:
maximum $ map length ["one", "two", "three", "four", "five"]
如何修改前面的语句来返回字符串 3 ?
How would I modify the previous statement to return the string three?
推荐答案
使用 maximumBy
, on
和 compare
你可以这样写表达式:
Using maximumBy
, on
and compare
you can write the expression like this:
import Data.List (maximumBy)
import Data.Function (on)
maximumBy (compare `on` length) ["one", "two", "three", "four", "five"]
这篇关于如何找到列表中最长的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文