CMPU-145, Spring 2013 Lab 6 March 25, 2013 Create a lab6 directory within your own account. Then download the files "asmt-helper.txt" and "lab5-solns-defns.txt" from the lab6 directory on the CMPU-145 web site. Open up drscheme and make sure that you include the following line near the beginning of your definitions file: (load "lab5-solns-defns.txt") That will load the functions defined during lab 5 (e.g., nn-succ, nn-addn, etc.) and will also take care of loading "asmt-helper.txt" for you. --------------- Recall the property called GEORGE that we proved in class: 0 + x = x for all natural numbers x. The following function, called TEST-GEORGE, uses the built-in RANDOM function and the DOTIMES special form to test the above equality for a bunch of randomly chosen values of x. (define test-george (lambda (n) ;; In the following expression, the variable I takes ;; on the values 0, 1, 2, ..., n-1. For each value of I, ;; the "body" of the DOTIMES (i.e., the LET* expression) ;; is evaluated. (dotimes (i n) ;; RND = a random number from 0 to 99 ;; NN = the corresponding list of ones (let* ((rnd (random 100)) (nn (make-nn rnd))) ;; Print out RND and either #t or #f ;; (#t indicates success; #f failure) (printf "[~A: ~A] " rnd (equal? (nn-addn nn0 nn) nn)))) (newline))) ===> Evaluate several expressions such as (test-george 10) in the Interactions Window. PROBLEM 1. Recall the property, called CRACKIE: x + Sy = Sx + y for all natural numbers x and y. Define a function, called TEST-CRACKIE, that uses RANDOM, LET* and DOTIMES to test that the above property holds for the NN-ADDN function defined during Lab 5. The TEST-CRACKIE function should take a single input, N, that determines how many tests the function will perform. For each test, use LET* to create some local variables: RNDX = a random number from 0 to 99 RNDY = a (probably different) random number from 0 to 99 NNX = the list-of-ones corresponding to RNDX NNY = the list-of-ones corresponding to RNDY Then use PRINTF to print out the values of RNDX, RNDY, and either #t or #f, depending upon whether the CRACKIE equation holds for the "values" of NNX and NNY. Be sure to use the NN-SUCC and NN-ADDN functions defined in lab5-solns-defns.txt. Use TEST-GEORGE as a model. Call the TEST-CRACKIE function using an input of at least 20. Verify that all tests came out #t. Note: Your output should look something like this: > (test-crackie 10) [95,48: #t] [92,2: #t] [61,89: #t] ... etc. ... (Of course, your randomly generated numbers will be different.) PROBLEM 4. Define a function, NN-MULT, that satisfies the following contract. ;;; 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 It should behave as illustrated below: > (nn-mult nn2 nn3) (1 1 1 1 1 1) > (nn-mult '(1 1 1) '(1 1 1 1)) (1 1 1 1 1 1 1 1 1 1 1 1) Following the style of the definition of NN-ADDN in Lab 5, your definition of NN-MULT should use NN-ZERO?, NN-ADDN and NN-PRED. ============================================= When you are done, ask a coach to come over to check your work. Then submit electronically using submit145, as usual.