;; **************************************** ;; * Csu 211 : 3/26/2008 ;; * Lecture #29 Code ;; * More Generative Recursion ;; **************************************** ;; kth: (listof Number) Number -> (listof Number) ;; Collect each of the kth index numbers in the list: k, 2*k, 3*k ... (define (kth lon k) (local ((define (kth* lon i) (cond [(empty? lon) empty] [(= i 1) (cons (first lon) (kth* (rest lon) k))] [else (kth* (rest lon) (- i 1))]))) (kth* lon k))) (check-expect (kth '(1 2 3 4 5 6 7 8) 1) '(1 2 3 4 5 6 7 8)) (check-expect (kth '(1 2 3 4 5 6 7 8) 2) '(2 4 6 8)) (check-expect (kth '(1 2 3 4 5 6 7 8) 4) '(4 8)) ;; first-k: (listof Number) Number -> (listof Number) ;; Get the first k numbers in a (listof Number) (define (first-k lon k) (cond [(= k 0) empty] [else (cons (first lon) (first-k (rest lon) (sub1 k)))])) (check-expect (first-k '(1 2 3 4 5 6 7) 1) '(1)) (check-expect (first-k '(1 2 3 4 5 6 7) 3) '(1 2 3)) (check-expect (first-k '(1 2 3 4 5 6 7) 5) '(1 2 3 4 5)) ;; without-first-k: (listof Number) Number -> (listof Number) ;; Remove the first 'k' elements from the given list (define (without-first-k lon k) (cond [(= k 0) lon] [else (without-first-k (rest lon) (sub1 k))])) (check-expect (without-first-k '(1 2 3 4 5 6 7) 1) '(2 3 4 5 6 7)) (check-expect (without-first-k '(1 2 3 4 5 6 7) 3) '(4 5 6 7)) (check-expect (without-first-k '(1 2 3 4 5 6 7) 5) '(6 7)) ;; qsort: (listof X) (X X -> X) -> (listof X) ;; Sort the given List in the order defined by 'op' using the ;; quick-sort algorithm (define (qsort lox op) (local ((define len (length lox))) (cond [(<= len 1) lox] [else (local ((define pivot (list-ref lox (random len))) (define (oped x) (op x pivot)) (define (eq x) (= x pivot)) (define (noped x) (and (not (op x pivot)) (not (= x pivot)))) (define oped-l (filter oped lox)) (define eq-l (filter eq lox)) (define noped-l (filter noped lox))) (append (qsort oped-l op) eq-l (qsort noped-l op)))]))) (check-expect (qsort '(5 3 2 1 0 9 8 7 6) <) '(0 1 2 3 5 6 7 8 9)) (check-expect (qsort '(5 3 2 1 0 9 8 7 6) >) '(9 8 7 6 5 3 2 1 0)) ;; bubble: (listof X) (X X -> X) -> (listof X) ;; Sort the given List in the order defined by 'op' using the ;; bubble-sort algorithm (define (bubble lox op) (local (;; bub*: (listof X) Number -> (listof X) ;; Sort the unsorted portion of sorted/2 (define (bub* sorted/2 unsorted#) (cond [(= unsorted# 0) sorted/2] [else (bub* (bub-one sorted/2 0 unsorted#) (- unsorted# 1))])) ;; bub-one: (listof X) Number Number -> (listof X) ;; Bubble the best X in the list to the back... (define (bub-one lst i unsorted#) (cond [(= i (- unsorted# 1)) lst] [(op (first lst) (second lst)) (cons (first lst) (bub-one (rest lst) (+ i 1) unsorted#))] [else (cons (second lst) (bub-one (cons (first lst) (rest (rest lst))) (+ i 1) unsorted#))]))) (cond [(empty? lox) empty] [else (bub* lox (length lox))]))) (check-expect (bubble '(5 3 2 1 0 9 8 7 6) <) '(0 1 2 3 5 6 7 8 9)) (check-expect (bubble '(5 3 2 1 0 9 8 7 6) >) '(9 8 7 6 5 3 2 1 0)) ;; the-best: (listof X) (X X -> X) -> X ;; Return the best X in the list, given a comparison operator (define (the-best lox better?) (local ((define (best* lox b-so-far) (cond [(empty? lox) b-so-far] [(better? (first lox) b-so-far) (best* (rest lox) (first lox))] [else (best* (rest lox) b-so-far)]))) (best* (rest lox) (first lox)))) (check-expect (the-best '(1 4 2 6 32 3 5 6 3 5 4) <) 1) (check-expect (the-best '(1 4 2 6 32 3 5 6 3 5 4) >) 32) (check-expect (the-best '(1 4 2 6 32 3 5 6 3 5 4) (lambda (n m) (= n 6))) 6) ;; Generate the test report (generate-report)