3 ms·
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 o
by silentbicycle 18y ago
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 the OCaml toplevel:
# let lwse = lazy (print_string "(Side effect)\n"; true);;
val lwse : bool lazy_t = <lazy>
# lwse;;
- : bool lazy_t = <lazy>
# Lazy.force lwse;;
(Side effect)
- : bool = true
# Lazy.force lwse;;
- : bool = true