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

From: Ian Tegebo (ian.tegebo at gmail.com)
Date: Sun Aug 19 00:59:28 EDT 2012

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.

That's when I encountered a possible (minor) bug with 'take' vs
'lazy/take'.  They appear to have their arguments swapped:

> (take '(a b) 1)
> (require (only-in lazy [take lazy-take]))
> (lazy-take '(a b) 1)
. . take: expects type <non-negative exact integer> as 1st argument,
given: '(a b); other arguments were: 1

My current approach aside, is there a better way?

Here's my workaround,

(define (call-n-times f n)
    (define (helper f n lst)
      (if (< n 1)
          (helper f (- n 1) (cons (f) lst))))
    (helper f n '()))

(define (take-randoms k n)
    (call-n-times (curry random n) k))

Ian Tegebo

Posted on the users mailing list.