gpt4 book ai didi

algorithm - 函数式编程习惯用法,用于计算 racket/haskell 中不发生突变的最多 4 个数字

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:12:43 24 4
gpt4 key购买 nike

我在 4 个变量 a、b、c 和 d 中统计了一个字符串中四个字符出现的次数。

现在,我想知道哪个字符出现的次数最多。

我想要一个函数式编程习惯用法来解决这个问题。

在 Haskell 中解决它的一种方法如下 -

foldl (\(count1, char1) (count2, char2) -> if count1 > count2 then (count1, char1) else (count2, char2)) (a, "A")  (zip [b, c, d] ["B", "C", "D"])

有人有其他函数式编程习惯用法来解决这个问题吗?

最佳答案

在 Haskell 中,惯用的方法是使用 maximumBy:

Data.List Data.Ord> snd . maximumBy (comparing fst) $ zip [4,3,7,1] "abcd"
'c'

关于algorithm - 函数式编程习惯用法,用于计算 racket/haskell 中不发生突变的最多 4 个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34958735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com