[racket-dev] 2htdp/image Feature Suggestion

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Mon Jun 23 22:39:17 EDT 2014

FWIW, I think this is what the style guide would recommend. I found
the transformation to be pretty straightforward, except that I had to
rename on occurrence of 'H' to 'H2'. (I didn't try to test it, tho!)

Robby

(define (make-natural->rearrangement L (EQ? equal?))
  (define N (nr-of-rearrangements L EQ?))
  (λ (K)
    (let rearrange ((L L) (K K) (N N) (result '()))
      ; Look for the K-th rearrangement of L and put it's elements in result.
      (cond
        [(null? L) result]
        [else
         (let pseudo-rotate ((H L) (T '()) (K K))
           ; Look in subsequent pseudorotations.
           (define E (car H))
           (define H2 (cdr H))
           (cond
             [(member E T EQ?)
              ; Skip pseudorotation if it's car already was car of a
previous one.
              (pseudo-rotate H2 (cons E T) K)]
             [else
              (define M (/ (* N (count-occurrences E L EQ?)) (length L)))
              ; M is the nr of rearrangements of (append H T)
              ; computed by means of a recurrent relation.
              (cond
                [(< K M)
                 ; The rearrangement is in this pseudorotation.
                 (rearrange (append H2 T) K M (cons E result))]
                [else
                 ; The rearrangement is not in this pseudorotation.
                 ; Look in the following pseudorotation, but decrease K by the
                 ; nr of rearrangements of the skipped pseudorotation.
                 (pseudo-rotate H2 (cons E T) (- K M))])]))]))))


Posted on the dev mailing list.