[Haskell-beginners] Red-black tree performance

Heinrich Apfelmus apfelmus at quantentunnel.de
Tue Mar 20 18:30:04 CET 2012


Lorenzo Bolla wrote:
> Your scripts stack-overflows on my box.
> 
> I've made 2 little changes, to ensure strictness. I believe your
> "insertList" is building up a huge list of thunks.
> 
> import Data.Foldable (foldr')
> <snip>
> main :: IO ()
> main = putStrLn $ show $ maxTree $ foldr' insert Empty toInsert
> 
> Better?

Huh, why foldr' and not Data.List.foldl' ? As far as I understand, the 
former has to evaluate the whole spine of the list first before it can 
begin to assemble the tree. The latter can start building trees right away.

I'm not sure whether the integers in  toInsert  are evaluated to WHNF. 
You can make the  insert  function strict in the first argument to make 
sure that they are.


Best regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com




More information about the Beginners mailing list