gre*_*emo 1 haskell functional-programming fold higher-order-functions
从前奏:
foldl1:它接受列表的前两项并将函数应用于它们,然后使用此结果和第三个参数提供函数,依此类推.
为什么不能写这样的东西?
foldl1 (==) [6, 6, 6]
foldl1 (\x y -> x == y) [6, 6, 6]
Run Code Online (Sandbox Code Playgroud)
如果要检查列表中的所有元素是否相等,可以快速解决
allEqual [] = True --(edit: this case is not necessary as pointed out by sepp2k)
allEqual xs = all (== head xs) xs
Run Code Online (Sandbox Code Playgroud)
我确信有人会写一个更优雅的方法,但这是可用的.
编辑:感谢sepp2k的建议.