Using Haskell's map function to calculate the sum of a list

前端 未结 8 2028
太阳男子
太阳男子 2020-11-28 16:29

Haskell

addm::[Int]->Int
addm (x:xs) = sum(x:xs)

I was able to achieve to get a sum of a list using sum fu

8条回答
  •  自闭症患者
    2020-11-28 16:53

    I realize this question has been answered, but I wanted to add this thought...

    listLen2 :: [a] -> Int
    listLen2 = sum . map (const 1)
    

    I believe it returns the constant 1 for each item in the list, and returns the sum! Might not be the best coding practice, but it was an example my professor gave to us students that seems to relate to this question well.

提交回复
热议问题