给定一个list L和一个函数f,我希望从L返回一个项目x,该项目将f(x)的值最大化。
我给出了两个变量的代码:
argmax f x y = if (f x) > (f y) then x else y这怎么能推广到
argmax f [x,y,z](列表中有任意数量的项目)
这就是我在Python中要做的
argmax = lambda f,L: max([(f(x),x) for x in L])[1] if len(L)>0 else None如何在Haskell中应用相同的逻辑?
发布于 2016-10-25 15:56:21
免责声明--我已经学习Haskell (一般功能范式)一周了,所以可能有更好的解决方案。
import Data.Ord
import Data.List
argmax f l
| null l = error "Given list is empty"
| otherwise = maximumBy (comparing f) lhttps://stackoverflow.com/questions/40244055
复制相似问题