A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Maximum Subarray in Haskell page! Here, you'll find the source code for this program as well as a description of how the program works.
module Main where
import System.Environment
import Text.Read
headMaybe :: [a] -> Maybe a
headMaybe [] = Nothing
headMaybe (x:xs) = Just x
-- Converts string in format "1, 2, 3" to a Maybe list of int
stringToListMaybe :: String -> Maybe [Int]
stringToListMaybe str = readMaybe $ "[" ++ str ++ "]" :: Maybe [Int]
verifyListNotEmpty :: [a] -> Maybe [a]
verifyListNotEmpty [] = Nothing
verifyListNotEmpty xs = Just xs
main :: IO ()
main = do
args <- getArgs
let xs = headMaybe args >>= stringToListMaybe >>= verifyListNotEmpty
case xs of
Just xs -> print $ maximum $ scanr1 (\x acc -> if acc < 0 then x else x + acc) xs
Nothing -> putStrLn "Usage: Please provide a list of integers in the format: \"1, 2, 3, 4, 5\""
Maximum Subarray in Haskell was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.