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,
Given your example, I would write the function something like this:
partialAdd :: [Int] -> [Int] -> [Int]
partialAdd ls seps = foldr f [] ls
where
f a [] = [a]
f a (x:xs)
| a `elem` seps = a:x:xs
| otherwise = (x+a):xs
*Main> partialAdd [1,2,3,4,5,6] [2,5]
[3,12,6]
Btw. I think the solution in your question seems not to work quite the way you specified in your example (or I misunderstood something):
partial_add :: [Int] -> [Int] -> Int -> [Int]
partial_add [] _ count = []
partial_add (a:x) list count | elem a list = count:partial_add x list 0
| otherwise = partial_add x list (count+a)
*Main> partial_add [1,2,3,4,5,6] [2,5] 0
[1,7]
But it is easily fixed to work for your example:
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)
*Main> partial_add [1,2,3,4,5,6] [2,5] 0
[3,12,6]