Personal tools

Haskell Quiz/Maximum Sub-Array/Solution Ninju

From HaskellWiki

Jump to: navigation, search

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)