[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/0UFUJWc0    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Mar 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; dutch national flag

(define examine vector-ref)

(define (swap! xs p1 p2)
  (let ((t (vector-ref xs p1)))
    (vector-set! xs p1 (vector-ref xs p2))
    (vector-set! xs p2 t))
  xs)

(define (dutch-national-flag xs)
  (let loop ((r 0) (w 0) (b (- (vector-length xs) 1)))
    (if (< b w) xs
      (case (examine xs w)
        ((#\R) (set! xs (swap! xs r w))
                (loop (+ r 1) (+ w 1) b))
        ((#\W) (loop r (+ w 1) b))
        ((#\B) (set! xs (swap! xs b w))
                 (loop r w (- b 1)))))))

(display (dutch-national-flag #(#\B #\W #\B #\R #\W #\R #\B)))


Output:
1
#(R R W W B B B)


Create a new paste based on this one


Comments: