In GHCi let y = y + 1
compiles fine, but when I try to evaluate y I got *** Exception: <<loop>>
Why is there no compile error and what does it mean <<loop>>
?
Haskell let
, where
and top-level bindings are recursive by default, even if they're not for a function. So let y = y + 1
defines an infinite loop of adding 1
to a number. GHC represents loops like this with the <<loop>>
exception—if it can catch them, of course!
This is usable for lazy operations, because it allows us to easily define things like infinite lists (let xs = 0:xs
), which are well-defined and actually useful for normal code. However, it can't work for strict operations like +
(for most numeric types) because they require evaluating the whole (infinite) thing right away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With