Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 0.48 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Can a thunk be duplicated to improve memory performance?
  2. let xs = [1..10000000]
  3.        
  4. bad = do
  5.     print $ foldl1' (+) xs
  6.     print $ length xs
  7.        
  8. good = do
  9.     (xs1,xs2) <- copyThunk xs
  10.     print $ foldl1' (+) xs1
  11.     print $ length xs2
  12.        
  13. xsFunction :: () -> [Int]
  14. xsFunction = const [1..10000000]
  15.  
  16. better = do
  17.   print $ foldl1' (+) $ xsFunction ()
  18.   print $ length $ xsFunction ()
  19.        
  20. let xs () = [1..1000000]
  21.  
  22. good = do
  23.     print $ foldl1' (+) (xs ())
  24.     print $ length (xs ())