Haskell Quiz/Maximum Sub-Array/Solution Ninju
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
No clever stuff here unfortunately.
module Main where
import System.Environment
import Data.List
import Data.Ord
main :: IO ()
main = do args <- getArgs
let array = map read args
let maxSubArray = maximumSubArray array
putStrLn $ "The maximum sub array in " ++ show array ++ " is " ++ show maxSubArray
return ()
maximumSubArray :: [Integer] -> [Integer]
maximumSubArray = last . sortBy (comparing sum) . slices
-- returns all sub lists of a given list not including the empty list
-- e.g. slices [1,2,3] = [[1], [1,2], [1,2,3], [2], [2,3], [3]]
slices :: Eq a => [a] -> [[a]]
slices [] = []
slices xs = tail $ inits xs ++ slices (tail xs)