Personal tools

Haskell Quiz/Maximum Sub-Array/Solution Jkramar

From HaskellWiki

< Haskell Quiz | Maximum Sub-Array(Difference between revisions)
Jump to: navigation, search
m
m
Line 2: Line 2:
   
 
<haskell>
 
<haskell>
  +
maxSubArray :: (Num a, Ord a) => [a] -> [a]
 
maxSubArray xs = drop from$take to xs where
 
maxSubArray xs = drop from$take to xs where
 
sumswithpos = zip (scanl (+) 0 xs) [0..]
 
sumswithpos = zip (scanl (+) 0 xs) [0..]

Revision as of 20:13, 19 November 2008


maxSubArray :: (Num a, Ord a) => [a] -> [a]
maxSubArray xs = drop from$take to xs where
  sumswithpos = zip (scanl (+) 0 xs) [0..]
  diff ((a,ai),(b,bi)) = (a-b,(bi,ai))
  (from,to) = snd$maximum$map diff$zip sumswithpos$scanl1 min sumswithpos