Haskell partial sum of a list error

后端 未结 3 1321
时光说笑
时光说笑 2021-01-20 19:32

I need to write a function in Haskell that sums the elements of a list until some specific elements stored in another list.

For example partial_add [1,2,3,4,5,

3条回答
  •  野性不改
    2021-01-20 20:02

    Thank to your answers i figured it out. I needed to declare the type of the function and put the parenthesis where they should be. The code was indeed not working as it should but i fixed that to.

    Here is the fixed code:

    partial_add :: [Int] -> [Int] -> Int -> [Int]        
    partial_add [] _ count = [count]
    partial_add (a:x) list count | elem a list =  (count+a):partial_add x list 0
                                 | otherwise =  partial_add x list (count+a)
    

    It may not be the best possible but it worked out for me.

提交回复
热议问题