Skip to content

Instantly share code, notes, and snippets.

@cronin101
Last active December 23, 2015 06:19
Show Gist options
  • Select an option

  • Save cronin101/6593647 to your computer and use it in GitHub Desktop.

Select an option

Save cronin101/6593647 to your computer and use it in GitHub Desktop.
Finding the slice of an input array that has the largest total
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