如何总结haskell中奇数的索引?

时间:2019-01-20 17:28:48

标签: haskell

数量为奇数的项目 定义函数f4,它代表列表中唯一索引的元素之和!索引是例外!

f4 :: [Int] -> Int
f4 [] == 0
f4 [4] == 4
f4 [4.2] == 4
f4 [4,2,3] == 7
f4 [4,2,3,5] == 7
f4 [5,4,2,3,8,3,9,5,2] == 26

f4 :: [Int] -> Int
f4 [] = 0
f4 [x] = x
f4 (x:xs) = odd (x:xs) 

1 个答案:

答案 0 :(得分:1)

根据我对控制台输出的了解,此功能可以为您提供帮助:

sumOdds :: [Double] -> Int
sumOdds []  = 0
sumOdds [a] = round a
sumOdds (x:xs) = round x + sumOdds (tail xs)