Using a filtered list in a new function in haskell

后端 未结 3 761
再見小時候
再見小時候 2021-01-19 05:56

So i\'m not too sure how to phrase this properly, but say I wanted to get the sum of all odd numbers in a list, do I have two functions (sumList and getOddNumbers) and combi

3条回答
  •  时光取名叫无心
    2021-01-19 06:44

    In the following are three equivalent ways to write the function oddSum :: [Integer] -> Integer:

    oddSum xs = sumList (getOddNumbers xs)
    
    oddSum xs = sumList $ getOddNumbers xs
    
    oddSum = sumList . getOddNumbers
    

    Btw, have a look at the filter and sum functions in the Prelude with which you could replace getOddNumbers and sumList respectively.

提交回复
热议问题