;;; ==================================== ;;; CMPU-145, Spring 2013 ;;; Lab 6 Solutions ;;; March 25, 2013 ;;; ==================================== (load "lab5-solns-defns.txt") (header "Solutions!" "Lab 6") (problem "Sample: George Testing!") ;;; TEST-GEORGE (i.e., whether 0 + x = x) ;;; --------------------------------------- ;;; INPUT: N, the number of tests to do ;;; OUTPUT: None ;;; SIDE EFFECT: Prints out the results of N random tests of the GEORGE ;;; property. In particular, for each test, it generates ;;; a random number from 0 to 99, converts it to a list-of-ones ;;; natural number NN, and then tests whether 0 + NN = NN for ;;; that value (using the NN-ADDN function). (define test-george (lambda (n) ;; Run N tests; the value of I goes from 0 to N-1 (dotimes (i n) (let* (;; RND is a random number from 0 to 99 (rnd (random 100)) ;; NN is the corresponding list-of-ones (nn (make-nn rnd))) ;; Print out the value of RND and #t or #f (depending on ;; whether the GEORGE property holds or not) (printf "[~A: ~A] " rnd (equal? (nn-addn nn0 nn) nn)))) (newline))) (tester '(test-george 10)) (newline) (tester '(test-george 20)) ;;; ========================================================== (problem "1: Test CRACKIE (i.e., whether Sx + y = x + Sy)") ;;; TEST-CRACKIE (i.e., whether Sx + y = x + Sy) ;;; -------------------------------------------------------- ;;; INPUT: N, the number of tests to do ;;; OUTPUT: None ;;; SIDE EFFECT: Prints out the results of N random tests of the CRACKIE ;;; property. In particular, for each test, it generates ;;; two random numbers, RNDX and RNDY, from 0 to 99, converts them ;;; to the corresponding lists-of-ones, NNX and NNY, and then tests ;;; whether S(NNX) + NNY = NNX + S(NNY) for those numbers, using ;;; the NN-ADDN function. (define test-crackie (lambda (n) (dotimes (i n) (let* (;; RNDX and RNDY are random numbers from 0 to 99 (rndx (random 100)) (rndy (random 100)) ;; NNX and NNY are the corresponding lists-of-ones (nnx (make-nn rndx)) (nny (make-nn rndy))) ;; Print out the values of RNDX and RNDY and whether ;; the CRACKIE property holds for NNX and NNY. (printf "[~A,~A: ~A] " rndx rndy (equal? (nn-addn (nn-succ nnx) nny) (nn-addn nnx (nn-succ nny)))))) (newline))) (tester '(test-crackie 10)) (newline) (tester '(test-crackie 20)) ;;; ==================================================================== (problem "3: NN-MULT") ;;; NN-MULT ;;; ------------------------------------------- ;;; INPUTS: NNX, NNY, two natural numbers represented as lists of ones ;;; OUTPUT: The product of NNX and NNY represented as a list of ones ;;; Uses the following rules: ;;; x * 0 = 0 ;;; x * S(y) = (x * y) + x (define nn-mult (lambda (nnx nny) (cond ;; Base Case: NNY = 0 ((nn-zero? nny) ;; NNX * 0 = 0 nn0) ;; Recursive Case: NNY > 0 (#t (nn-addn (nn-mult nnx (nn-pred nny)) nnx))))) (tester '(nn-mult nn2 nn3)) (tester '(nn-mult nn0 nn4)) (tester '(nn-mult nn4 nn0)) (tester '(nn-mult nn3 nn4)) (tester '(length (nn-mult nn4 nn4))) (tester '(length (nn-mult (make-nn 32) (make-nn 10))))