Last active
December 23, 2015 06:19
-
-
Save cronin101/6593647 to your computer and use it in GitHub Desktop.
Finding the slice of an input array that has the largest total
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.List | |
| data SummedIntList = SummedIntList { | |
| listElements :: [Int], | |
| elementTotal :: Int | |
| } deriving (Show) | |
| emptySummedList = SummedIntList [] 0 | |
| addIntToList (SummedIntList xs t) x = SummedIntList (xs ++ [x]) (t + x) | |
| largestSlice = scanList emptySummedList emptySummedList | |
| where | |
| scanList store curr [] = largestElements store curr | |
| scanList store curr (x:xs) | |
| | x >= 0 = scanList store (addIntToList curr x) xs | |
| | otherwise = scanList (largest store curr) emptySummedList xs | |
| largest a b | |
| | elementTotal a > elementTotal b = a | |
| | otherwise = b | |
| largestElements = (listElements . ) . largest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment