#3416: maximumBy uses too much stack
-----------------------------+----------------------------------------------
Reporter:  bdonlan           |          Owner:                  
    Type:  bug               |         Status:  new             
Priority:  normal            |      Component:  libraries/base  
 Version:  6.10.4            |       Severity:  normal          
Keywords:                    |       Testcase:                  
      Os:  Unknown/Multiple  |   Architecture:  Unknown/Multiple
-----------------------------+----------------------------------------------
 maximumBy uses too much stack when working on long lists:

 {{{
 Prelude Data.List> maximumBy compare [1..1000000]
 *** Exception: stack overflow
 }}}

 Here's a fixed implementation; the only change is replacing foldl1 with
 foldl1'.

 {{{
 -- | The 'maximumBy' function takes a comparison function and a list
 -- and returns the greatest element of the list by the comparison
 function.
 -- The list must be finite and non-empty.
 maximumBy               :: (a -> a -> Ordering) -> [a] -> a
 maximumBy _ []          =  error "List.maximumBy: empty list"
 maximumBy cmp xs        =  foldl1' maxBy xs
                         where
                            maxBy x y = case cmp x y of
                                        GT -> x
                                        _  -> y
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3416>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to