;;; ========================== ;;; CMPU-145, Spring 2013 ;;; asmt6-helper.txt ;;; ========================== ;;; Loads asmt5-helper.txt and then defines some functions needed ;;; for asmt6. (load "asmt5-helper.txt") ;;; TESTER-SHOW-LIST ;;; ----------------------------------------- ;;; INPUT: An expression that evaluates to a list ;;; OUTPUT: Prints out that expression verbatim, and ;;; then uses a helper function to display the contents ;;; of the list to which that expression evaluates. ;;; Example: (tester-show-list '(cons 1 (cons 2 ()))) ;;; (define *my-listy* '(1 2 3 4)) ;;; (tester-show-list '*my-listy*) (define tester-show-list (lambda (quoted-listy) (printf "~A ==>~%" quoted-listy) (tester-show-list-helper (eval quoted-listy)))) ;;; TESTER-SHOW-LIST-HELPER ;;; ----------------------------------- ;;; Prints contents of input list on separate lines. (define tester-show-list-helper (lambda (listy) (cond ((null? listy) (newline)) (#t (printf "~A~%" (first listy)) (tester-show-list-helper (rest listy)))))) ;;; *PB-59* -- The set of numbers from which the first five numbers on ;;; a powerball ticket are drawn (define *pb-59* '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59)) ;;; *PB-35* -- The set of numbers from which a powerball is chosen (define *pb-35* '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35)) ;;; GEN-RAND-PB-TICKET ;;; --------------------------------------- ;;; INPUTS: None ;;; OUTPUT: A list of six numbers: the first five are chosen ;;; from (1,2,3,...,59) (no replacement); the last one is chosen ;;; from (1,2,3,...,35) (define gen-rand-pb-ticket (lambda () ;; Note how we CONS the powerball onto the FRONT of the list, ;; and then REVERSE it to place the powerball at the end!! (reverse (cons ;; Pick the powerball (from 1 to 35) (+ 1 (random 35)) ;; Pick the five numbers from 1 to 59 (rand-choose-k 5 *pb-59*))))) ;;; NUM-MATCHES ;;; --------------------------------- ;;; INPUTS: LISTY, LISTZ, two lists ;;; OUTPUT: The number of elements of LISTY that also appear in LISTZ ;;; (same as the number of elements in the intersection of the two lists) (define num-matches (lambda (listy listz) (cond ;; Base Case: LISTY is empty ((null? listy) ;; So no matches 0) ;; Recursive Case 1: We found a match! ((member (first listy) listz) ;; So add 1 to the recursive function call on the rest of LISTY (+ 1 (num-matches (rest listy) listz))) ;; Recursive Case 2: We did not find a match! (#t ;; So just focus on the REST of listy (num-matches (rest listy) listz))))) (define *draw-ticket* '(1 2 3 4 5 20))