im looking for a solution for my Haskell class.
I have a list of numbers, and I need to return SUM for each part of the list. Parts are divided by 0. I need to use the FOLDL function.
Example:
initial list: [1,2,3,0,3,4,0,5,2,1]
sublist [[1,2,3], [3,4], [5,2,1]]
result [6,7,7]
I have a function to find 0 in the source list:
findPos list = [index+1 | (index, e) <- zip [0..] list, e == 0]
(returns the source list from the example) [4,6]
and function to create SUM with FOLDL:
sumList list = foldl (+) 0 list
But I was completely unable to collect it: /
---- MY DECISION
In the end, I found something completely different that you guys suggested.
Took me all day to do this: /
groups :: [Int] -> [Int]
groups list = [sum x | x <- makelist list]
makelist :: [Int] -> [[Int]]
makelist xs = reverse (foldl (\acc x -> zero x acc) [[]] xs)
zero :: Int -> [[Int]] -> [[Int]]
zero x acc | x == 0 = addnewtolist acc
| otherwise = addtolist x acc
addtolist :: Int -> [[Int]] -> [[Int]]
addtolist i listlist = (i : (head listlist)) : (drop 1 listlist)
addnewtolist :: [[Int]] -> [[Int]]
addnewtolist listlist = [] : listlist