Scheme shuffle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ; shuffled: nat -> list
; returns a list of numbers from one to n (inclusive) in a
; random order
(define (shuffled n)
(local
[(define l (build-list n add1))
(define (pull new old)
(if (empty? old)
new
(local
[(define rand (list-ref old (random (length old))))]
(pull
(cons rand new)
(filter (lambda (x) (not (= x rand))) old)))))]
(pull empty (build-list n add1))))
|