[racket] taking from a generator, and a possible bug

From: Danny Yoo (dyoo at hashcollision.org)
Date: Sun Aug 19 01:56:23 EDT 2012

On Sunday, August 19, 2012, Ian Tegebo wrote:

> I want to,
>
> (take k (in-generator (infinite-generator (yield (random n)))))
>
> where k and n are positive integers, k<n.
>
> Unfortunately, 'in-generator' returns a sequence while 'take' needs a
> list and calling 'sequence->list' results in an error.  So, I thought
> maybe I needed 'take' from 'lazy'.  But it also complains about the
> index being too large.
>


The take function works on lists, and although what you have in hand is a
sequence, not all sequences are lists.

You should be able to take the first k elements of that sequence like this:

;;;;;;
(for/list ([elt (in-generator (infinite-generator (yield (random n))))]
         [i k])
   elt)
;;;;;;

There may be an easier way to to do this, but using the for/list loop
should do the trick.  Good luck!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120819/8d9bcde9/attachment.html>

Posted on the users mailing list.