Computing the mean of a list efficiently in Haskell

后端 未结 6 1928
傲寒
傲寒 2021-02-04 11:21

I\'ve designed a function to compute the mean of a list. Although it works fine, but I think it may not be the best solution due to it takes two functions rather than one. Is it

6条回答
  •  情深已故
    2021-02-04 11:47

    Your solution is good, using two functions is not worse than one. Still, you might put the tail recursive function in a where clause.

    But if you want to do it in one line:

    calcMeanList = uncurry (/) . foldr (\e (s,c) -> (e+s,c+1)) (0,0)
    

提交回复
热议问题