[racket] how to use 'lazy' from racket/promise

From: Neil Toronto (neil.toronto at gmail.com)
Date: Tue Apr 9 21:25:39 EDT 2013

The following program creates four infinite lists and tries to take the 
first of each. Only the last expression raises an error.

#lang racket

(require racket/promise)

(define ones (lazy (cons 1 ones)))
(define ones* (delay (cons 1 ones*)))

(car (force ones))
(car (force ones*))

(define twos (lazy (lazy (cons 2 twos))))
(define twos* (delay (delay (cons 2 twos*))))

(car (force twos))
(car (force twos*))

The fact that compositions of `lazy' are equivalent to one `lazy' makes 
them easier to use in situations where you want to wrap a value with a 
promise but don't know whether it's already wrapped. Somehow, this makes 
it easier to implement lazy languages, but I'm not clear about how.

Neil ⊥

On 04/09/2013 06:05 PM, Lewis Brown wrote:
> Thanks, Danny.
> I am trying to understand how to use promises, in particular for lazy programming. I know there are various implementations that solve this more simply: racket/stream and racket/lazy; but I'd like to see how it's done with the more primitive racket/promise library. It appears that the "lazy" form is specifically meant for lazy, infinite lists, but the spec is not clear enough for me to figure out how to use it. I thought a couple examples would make it so.
> Lewis Brown
> lewisbrown at gmail.com
> 503-583-2332
> On 9 Apr 13, at 4:50 PM, Danny Yoo wrote:
>> Are you sure you're not looking for the racket/stream library instead?
>> For example:
>>     #lang racket
>>     (require racket/stream)
>>     (define ones (stream-cons 1 ones))
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users

Posted on the users mailing list.