News for hackers. Stuff that matters.
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 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

There are no comments on this comment yet.