Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

doing for loops assignment

Name: Anonymous 2010-10-24 12:10

I'm in a programming class and we have an assignment about for loops where we gotta shuffle a deck of cards. Can someone show me how to do this please? in return, tits

Name: Anonymous 2010-10-25 18:57

Assuming a vector,
(define (shuffle-vector! vector)
  (loop ((for element i (in-vector vector)))
        (let ((j (random-integer (+ i 1))))
          (vector-set! vector i (vector-ref vector j))
          (vector-set! vector j element))))


>>11
I don't like the way some Schemers (like the guy who does programming praxis) seem to thing that it's a good idea to use as many primitive abstractions as possible. I've rewritten your code to make it more palatable to me

(import (rnrs)
        (srfi :27 random-bits)
        (only (srfi :1 lists) split-at append-reverse)
        (srfi :8 receive)
        (wak foof-loop))

(define (half n)
  (floor (/ n 2)))

(define (flip-coin)
  (zero? (random-integer 2)))

(define (merge picker list1 list2)
  (loop continue ((for car1 cons1 (in-list list1))
                  (for car2 cons2 (in-list list2))
                  (with result '()))
        => (append-reverse result (append cons1 cons2))
        (if (picker car1 car2)
            (continue (=> result (cons car1 result))
                      (=> cons2 cons2))
            (continue (=> result (cons car2 result))
                      (=> cons1 cons1)))))

(define (shuffle l)
  (receive (head tail) (split-at l (half (length l)))
    (merge (lambda args (flip-coin)) head tail)))

(define (shuffle-many l c)
  (loop ((for num-times (down-from c (to 0)))
         (with l l (shuffle l)))
        => l))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List