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

From: Stephen Chang (stchang216 at gmail.com)
Date: Wed Apr 10 01:33:18 EDT 2013

Here's a program that prints the first 10 Fibonacci numbers:

#lang racket

(define (lazy-map f . lsts)
  (define flsts (map force lsts))
  (if (ormap null? flsts)
      null
      (cons (apply f (map car flsts))
            (lazy (apply lazy-map f (map cdr flsts))))))

(define fib (cons 1 (cons 1 (lazy (lazy-map + fib (cdr fib))))))

(let loop ([n 0] [fib fib])
  (unless (>= n 10)
    (define ffib (force fib))
    (displayln (car ffib))
    (loop (add1 n) (cdr ffib))))






On Tue, Apr 9, 2013 at 9:32 PM, Ryan Culpepper <ryanc at ccs.neu.edu> wrote:
> See SRFI-45 (http://srfi.schemers.org/srfi-45/srfi-45.html) for a discussion
> of why lazy is necessary (ie, why force and delay are not sufficient in
> practice).
>
> Racket's lazy might (?) be slightly different from the one discussed there,
> though. See this post by Eli for why:
> http://srfi.schemers.org/srfi-45/post-mail-archive/msg00013.html (according
> to comments in racket/private/promise.rkt).
>
> Ryan
>
>
> On 04/09/2013 09:25 PM, Neil Toronto wrote:
>>
>> 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
>>>
>>
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
>
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users


Posted on the users mailing list.