[racket] on reversing a list by way of sort

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Sep 17 00:01:45 EDT 2014

In Tue, Sep 16, 2014 at 11:08 PM, Daniel Prager
<daniel.a.prager at gmail.com> wrote:
> Here's a version of "inside-out" Fisher-Yates that should fit the bill:
>
> (define (fy-shuffle lst)
>   (define v (list->vector lst))
>   (for/list ([n (in-range (length lst) 0 -1)])
>     (let* ([i (sub1 n)]
>            [j (random n)]
>            [v_j (vector-ref v j)])
>       (when (not (= i j))
>         (vector-set! v j (vector-ref v i)))
>       v_j)))

I was thinking more of a direct translation, which (I think) is easier
to see that it's correct:

    (define (shuffle l)
      (define a (make-vector (length l)))
      (for ([x (in-list l)] [i (in-naturals)])
        (define j (random (add1 i)))
        (unless (= j i) (vector-set! a i (vector-ref a j)))
        (vector-set! a j x))
      (vector->list a))

But the issues from my last email are still there.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.