-- Written by Mohit Jain
-- Print the maximum sum of a sub-list (contigous elements) in given list
-- Input may contain one or negative number
-- Input is empty or contains at least one positive number
main = printMaxSumOfSublist [4, -3, -2, 2, 3, -1, 3, -12, 5, -9, 0, -1]
printMaxSumOfSublist = putStr . show . findMaxSum
findMaxSum = optSumSubList 0 0
optSumSubList bestSumSoFar _ [] = bestSumSoFar
optSumSubList bestSumSoFar sumEndingHere x = optSumSubList newBest newSum remX where
remX = tail x
newBest = max bestSumSoFar newSum
newSum = max 0 sumWithEle
sumWithEle = sumEndingHere + head x