If your "insert" is idempotent, and you have some means of measuring
tree size, you can eliminate i, c and x:

(define tree4
  (do ([tree null (insert tree (random 10))])
    [(>= (tree-size tree) 5) tree]))

I arrived at this by considering doing something similar for sets:

(define tree4
  (do ([tree (set) (set-add tree (random 10))])
    [(>= (set-count tree) 5) tree]))



On 2016-02-15 1:58 AM, JJ wrote:
> I try to fill a binary tree with 5 random numbers, avoiding duplicates. Is 
> there a more elegant way than this (using "Iterations and Comprehensions")?
> 
> (define tree4
>   (do ([x (random 10) (random 10)]
>        [c #f (contains? tree x)]
>        [tree null (if c tree (insert tree x))]
>        [i 0 (if c i (add1 i))])
>     [(>= i 5) tree]))
> 
> Thanks!
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to