I have a bug I can’t really figure out. System is 10.5 (32 bits) with Haskell Platform 2010, GHC 6.12
Consider the following:
loop :: IO ()
loop = do
return ()
loop
main = do
loop
when compiled,
ghc --make test.hs
this code ends up taking 100% of the CPU’s time. Why is that?
How do you write a program in Haskell that loops like this while being nice to the laptop’s battery?
Concurrent’s “yield” does not seem to do anything interesting.
Same reason a tight loop in any language eats an entire CPU. You didn’t tell it to loop any slower than “as fast as it can”. Look at
Control.Concurrent‘sthreadDelay.