;; CMPU 145, Spring 2019 ;; Lab 7 Solutions #lang racket (include "asmt-helper.scm") (include "lab6-solns.scm") (header "Solutions" "Lab 7") (problem "Introduction") ;; GEN-AND-TEST ;; ------------ ;; INPUTS: N, a non-negative integer ;; GEN, a function of one input that is used to generate random ;; instances of some event (e.g., tossing a die, dealing a card) ;; TEST, a function that tests whether some given instance has some ;; property (e.g., die > three, or card is a face card) ;; OUTPUT: The fraction of N random trials in which the randomly generated ;; instances satisfied the TEST. (define gen-and-test (lambda (n gen test [print #f]) ;; COUNT is a counter variable (let ((count 0)) ;; FOR executes its body N times (for ((i n)) ;; Use (GEN) to generate a random instance; ;; Use TEST to determine whether that instance has the ;; desired property (let* ((rnd (gen)) (result (test rnd))) (when print (printf "[~A: ~A] " rnd result)) (when result ;; If it does, then destructively increment the counter! (set! count (+ count 1))))) ;; After the DOTIMES loop finishes, return the fraction of the ;; trials that satisfied the TEST. (/ count n 1.0)))) ;; TOSS-DIE ;; -------- ;; INPUTS: None ;; OUTPUT: A randomly generated number from 1 to 6 (define toss-die (lambda () (+ 1 (random 6)))) ;; TEST-DIE ;; -------- ;; INPUT: A number from 1 to 6 ;; OUTPUT: #t if that number is either 5 or 6 (define test-die (lambda (die) (or (= die 5) (= die 6)))) (tester '(gen-and-test 1000 toss-die test-die)) (tester '(gen-and-test 10000 toss-die test-die)) (newline) (problem "1. Pair of dice") ;; TOSS-PAIR-DICE ;; -------------- ;; INPUTS: None ;; OUTPUT: A two-element list containing a pair of randomly generated ;; numbers from 1 to 6 (define toss-pair-dice (lambda () ;; Note the use of TOSS-DIE (list (toss-die) (toss-die)))) ;; TEST-PAIR-DICE ;; -------------- ;; INPUT: A two-element list containing a pair of numbers each between 1 and 6 ;; OUTPUT: #t if the sum of those two numbers is at least 8 (define test-pair-dice (lambda (pr) (>= (+ (first pr) (second pr)) 8))) (tester '(gen-and-test 1000 toss-pair-dice test-pair-dice)) (tester '(gen-and-test 1000 toss-pair-dice test-pair-dice)) (tester '(gen-and-test 1000 toss-pair-dice test-pair-dice)) (tester '(gen-and-test 1000 toss-pair-dice test-pair-dice)) (newline) (tester '(gen-and-test 100000 toss-pair-dice test-pair-dice)) (problem "2. Properties of natural numbers") ;; Generate a natural number between 0 and 99, represented as a list of 1s (define random-nn (lambda () (make-nn (random 100)))) (define test-claim1 (lambda (nn) (equal? (nn-addn nn0 nn) nn))) (tester '(gen-and-test 10 random-nn test-claim1)) (tester '(gen-and-test 100 random-nn test-claim1)) (define random-nn-pair (lambda () (list (random-nn) (random-nn)))) (define test-claim2 (lambda (nn-pair) (let ((nnx (first nn-pair)) (nny (second nn-pair))) (equal? (nn-addn (nn-succ nnx) nny) (nn-addn nnx (nn-succ nny)))))) (tester '(gen-and-test 20 random-nn-pair test-claim2))