[racket] Space safety in lazy

From: Rodolfo Carvalho (rhcarvalho at gmail.com)
Date: Sun Feb 19 12:47:49 EST 2012

Thanks, Matthew.

On Sun, Feb 19, 2012 at 11:50, Matthew Flatt <mflatt at cs.utah.edu> wrote:

> Oops --- I should have tried that program more carefully.
> The problem seems to be with `or'. If I change to `if', then the
> example runs as intended:
>  (define (has-negative? l)
>   (if (negative? (car l))
>       #t
>       (has-negative? (rest l))))
> The `or' from `lazy' effectively adds a `!' around its last argument
> when `or' is applied directly, and it shouldn't do that.

Using `if' instead of `or' works.

But I don't understand when/why the force `!' is introduced or not.
For example, if I add a `set!' above the `if' expression, the program will
run out of memory:

#lang lazy

(define (list-from n)
  (cons n (list-from (add1 n))))

(define last-time-ms (current-inexact-milliseconds))

(define (has-negative? l)
  (set! last-time-ms (current-inexact-milliseconds))
  (if (negative? (car l))
      (has-negative? (rest l))))

(has-negative? (list-from 0))  ;; runs out of memory after a while

How to reason about the "laziness" of the program?

> (Another
> repair is to get `or' as a value, since the function form treats its
> last argument correctly.)

I didn't understand the above. What does it mean to get `or' as a value?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120219/627d9249/attachment.html>

Posted on the users mailing list.