Re:Structure and Interpretation of Computer Programs
by silentbicycle on Tuesday September 30, 2008 @03:22PM
(#319932)
You can use side effects with lazy evaluation, it's just that if you guarantee that you won't, various things suddenly become much easier to do safely (and to optimize for). It's a trade-off. Haskell is lazy by default, but you can also do lazy evaluation in Scheme and OCaml, for example.
In Scheme (PLT):
(define lazy-with-side-effect
(delay (begin (print "Hey, a side effect!")
#t)))
> lazy-with-side-effect
#<promise>
> (force lazy-with-side-effect)
"Hey, a side effect!"#t
> (force lazy-with-side-effect)
#t
In Scheme (PLT):
In the OCaml toplevel: