Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
40
Task/Sort-three-variables/Racket/sort-three-variables.rkt
Normal file
40
Task/Sort-three-variables/Racket/sort-three-variables.rkt
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#lang racket
|
||||
|
||||
(define-syntax-rule (sort-3! x y z <?)
|
||||
(begin
|
||||
(define-syntax-rule (swap! x y) (let ((tmp x)) (set! x y) (set! y tmp)))
|
||||
(define-syntax-rule (sort-2! x y) (when (<? y x) (swap! x y)))
|
||||
(sort-2! x y)
|
||||
(sort-2! x z)
|
||||
(sort-2! y z)))
|
||||
|
||||
(module+ test
|
||||
(require rackunit
|
||||
data/order)
|
||||
|
||||
(define (test-permutations l <?)
|
||||
(test-case
|
||||
(format "test-permutations ~a" (object-name <?))
|
||||
(for ((p (in-permutations l)))
|
||||
(match-define (list a b c) p)
|
||||
(sort-3! a b c <?)
|
||||
(check-equal? (list a b c) l))))
|
||||
|
||||
(test-permutations '(1 2 3) <)
|
||||
|
||||
;; string sorting
|
||||
(let ((x "lions, tigers, and")
|
||||
(y "bears, oh my!")
|
||||
(z "(from the \"Wizard of OZ\")"))
|
||||
(sort-3! x y z string<?)
|
||||
(for-each displayln (list x y z)))
|
||||
|
||||
(newline)
|
||||
|
||||
;; general data sorting
|
||||
(define datum<? (order-<? datum-order))
|
||||
(let ((x "lions, tigers, and")
|
||||
(y "bears, oh my!")
|
||||
(z '(from the "Wizard of OZ")))
|
||||
(sort-3! x y z datum<?)
|
||||
(for-each displayln (list x y z))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue