;; ;; Different types of random number lists ;; ;; ---------------------------------------------------------------------- ;; number-list: Number -> (listof Number) ;; produces a list with n numbers (define (number-list n) (build-list n (lambda (x) 0))) ;; Tests: note we're just intersted in the length (= (length (number-list 0)) 0) (= (length (number-list 3)) 3) ;; ---------------------------------------------------------------------- ;; distinct-number-list: Number -> (listof Number) ;; produces a list with n numbers (define (distinct-number-list n) (build-list n (lambda (x) x))) ;; Tests: note we're just testing whether the length is what ;; we want and that they are all distinct. We can use a helper ;; function ;; test-distinct-number-list: n ;; creates a list from calling distinct-number-list and retun true ;; if the length of that list is n and all numbers are distinct in ;; that list (define (test-distinct-number-list n) (local ((define lst (distinct-number-list n)) (define (all-distinct lst) (cond [(empty? lst) true] ;; is the first contained in the rest -- look up memv [(cons? (memv (first lst) (rest lst))) false] [else (all-distinct (rest lst))]))) (and (= (length lst) n) (all-distinct lst)))) (test-distinct-number-list 0) (test-distinct-number-list 3) ;; ---------------------------------------------------------------------- ;; random-number-list: Number -> (listof Number) ;; produces a list of n random numbers between 0 and n-1 (define (random-number-list n) (build-list n (lambda (x) (random n)))) ;; Tests: we an only test the length of the list (= (length (random-number-list 0)) 0) (= (length (random-number-list 3)) 3) ;; ---------------------------------------------------------------------- ;; contains: X (listof X) ;; true if x is in lox (define (contains x lox) (cons? (memv x lox))) ;; Tests (contains 1 '(1 2 3)) (not (contains 1 '(2 3 4))) ;; random-distinct-number-list: Number -> (listof Number) ;; produces a list of n random, distinct numbers between 0 and n-1 (define (random-distinct-number-list n) (random-distinct-number-list-accum n empty)) ;; random-distinct-number-list: Number -> (listof Number) ;; produces a list of n random, distinct numbers between 0 and n-1' ;; accumulates the result in acc (start with empty!) (define (random-distinct-number-list-accum n acc) (cond [(= (length acc) n) acc] [else (local ((define r (random n))) (random-distinct-number-list-accum n (if (contains r acc) acc (cons r acc))))])) ;; Tests: we'll use a helper function! ;; test-random-distinct-number-list: n ;; creates a list from calling distinct-number-list and retun true ;; if the length of that list is n and all numbers are distinct in ;; that list (define (test-random-distinct-number-list n) (local ((define lst (random-distinct-number-list n)) (define (all-distinct lst) (cond [(empty? lst) true] ;; is the first contained in the rest -- look up memv [(cons? (memv (first lst) (rest lst))) false] [else (all-distinct (rest lst))]))) (and (= (length lst) n) (all-distinct lst)))) (test-random-distinct-number-list 0) (test-random-distinct-number-list 3) ;; ;; I'll do this again, using local ;; Note the difference. The local function called loop (this is an idiom!) ;; doesn't take n as an argument, because we already know about it ;; ;; random-distinct-number-list: Number -> (listof Number) ;; produces a list of n random, distinct numbers between 0 and n-1 (define (random-distinct-number-list/local n) (local ((define (loop acc) (cond [(= (length acc) n) acc] [else (local ((define r (random n))) (loop (if (contains r acc) acc (cons r acc))))]))) (loop empty))) ;; Tests: we'll use a helper function! ;; test-random-distinct-number-list: n ;; creates a list from calling distinct-number-list and retun true ;; if the length of that list is n and all numbers are distinct in ;; that list (define (test-random-distinct-number-list/local n) (local ((define lst (random-distinct-number-list/local n)) (define (all-distinct lst) (cond [(empty? lst) true] ;; is the first contained in the rest -- look up memv [(cons? (memv (first lst) (rest lst))) false] [else (all-distinct (rest lst))]))) (and (= (length lst) n) (all-distinct lst)))) (test-random-distinct-number-list/local 0) (test-random-distinct-number-list/local 3)