;; CS101, Spring 2013 ;; Lab 6 ;; Using the random function and higher-order functions (display "\n CS101 Lab 6, Spring 2013\n") (display "\n PLEASE WRITE YOUR NAME HERE\n\n") (newline) (newline) (display "Problem 1: Simulate result of rolling three n-sided dice.\n\n") ; ; PROBLEM 1: Function ROLL-3-DICE ; ; Write a function that consumes 1 positive natural number, n, ; and that returns a possible result of rolling three n-sided dice. ; Remember that certain numbers occur with greater probability ; than others. ; ; The following is an example of what calling this function in ; the interactions pane might look like. You should try this a ; number of times for a given n after you have written the function ; to convince yourself that the numbers generated are sufficiently ; random. ; ; > (roll-3-dice 6) ; 7 ; > (roll-3-dice 8) ; 13 ; ; Pre-function tests for this function will not be possible due ; to random generation of list contents. You can, however, test ; the range of possible numbers in your check-expect statements, ; as shown below. ; ; Contract: ; Header: ; Purpose: ; Pre-function tests: ;(check-expect (and (>= (roll-3-dice 8) 3) (<= (roll-3-dice 8) 24)) true) ;(check-expect (or (< (roll-3-dice 8) 3) (> (roll-3-dice 8) 24)) false) ; Function definition: ; Post-function printf's: (newline) (newline) (display "Problem 2(a): Create a list of random numbers.\n\n") ; ; Problem 2(a): Function CREATE-RANDOM-LIST ; ; Write a recursive function that consumes 2 positive natural ; numbers, num and range, and that returns a list of num numbers ; that are chosen at random between 1 and range. ; ; The following is an example of what calling this function in ; the interactions pane might look like. The contents of the list ; will differ because the numbers in the list are chosen at ; random. ; ; > (create-random-list 10 20) ; (list 2 14 5 8 18 7 20 1 4 17) ; ; > (create-random-list 5 100) ; (list 98 3 27 59 42) ; ; > (create-random-list 0 0) ; empty ; ; Pre-function tests for this function will not be possible due ; to random generation of list contents. ; ; Use recursion in this solution. ; ; Warning: Calling the random function on 0 produces an error. ; ; A list of numbers (lon) is either: ; 1. empty, or ; 2. (cons num ln), where num is a number and ln is a lon ; Contract: ; Header: ; Purpose: ; Pre-function tests: ; Function definition: ; Post-function printf's: (newline) (newline) (display "Problem 2(b): Build a list of random numbers.\n\n") ; ; Problem 2(b): Function CREATE-RANDOM-BUILDLIST ; ; Write a function that consumes 2 positive natural numbers, ; num and range, and that returns a list of num numbers that are ; chosen at random between 1 and range. ; ; The following is an example of what calling this function in ; the interactions pane might look like. The contents of the list ; will differ because the numbers in the list are chosen at ; random. ; ; > (create-random-buildlist 10 20) ; (list 2 14 5 8 18 7 20 1 4 17) ; ; Use a call to the function build-list in the body of your function ; instead of using explicit recursion. ; ; Hint: The unnamed lambda function that is consumed by the ; build-list function may ignore its input parameter. ; ; Contract: (build-list number (X -> Y)) -> (listof Y) ; Header: (define build-list (lambda (n f) ... )) ; Purpose: Constructs a list by applying f, a single parameter ; function, to the numbers between 0 and (- n 1): ; ; Contract: ; Header: ; Purpose: ; Pre-function tests: ; Function definition: ; Post-function printf's: (newline) (newline) (display "Problem 3: FILTER-IF-NOT.\n\n") ; ; Problem 3: Function FILTER-IF-NOT ; ; Write a function called filter-if-not that takes 2 arguments: ; a predicate function and a listof anything. Filter-if-not should ; apply the predicate to every element on the list and return the ; elements for which the predicate is false. ; ; The recursive action of the filter-if-not function should be ; done using the built-in FILTER function. ; ; Examples of running this function: ; ; > (filter-if-not boolean? '(#t 4 a #f 43)) ; (list 4 'a 43) ; > (filter-if-not number? '(#t 4 a #f 43)) ; (list #t 'a #f) ; > (filter-if-not odd? '(1 2 3 4 5 6 7)) ; (list 2 4 6) ; > (filter-if-not even? '(1 2 3 4 5 6 7)) ; (list 2 4 6) ; ; The pre-function tests and post-function printfs are provided ; for you...uncomment them after you have written the function. ; ; A list of anything (lox) is either: ; 1. empty, or ; 2. (cons x lx), where x is anything and ls is a lox ; Contract: ; Header: ; Purpose: ; Pre-function tests: (uncomment these check-expects after you write ; the function) ;(check-expect (filter-if-not number? '(a 3 45 "hi" b c)) '(a "hi" b c)) ;(check-expect (filter-if-not number? '(43 21 67 34)) '()) ;(check-expect (filter-if-not number? '()) '()) ; Function definition: ; Post-function printf's: (uncomment these lines after you write the ; function) ;(printf "(filter-if-not number? '(a a 1 1 b b)) => ~a~%" ; (filter-if-not number? '(a a 1 1 b b))) ;(printf "(filter-if-not number? '(a a b b c 0)) => ~a~%" ; (filter-if-not number? '(a a b b c 0))) ;(printf "(filter-if-not number? '(12 3 4)) => ~a~%" ; (filter-if-not number? '(12 3 4))) (newline) (newline) (display "Problem 4: Average.\n\n") ; ; Problem 4: Function AVERAGE ; ; The average of the list (a_1 a_2 ... a_n) is defined ; to be the following quantity: ; (a_1 + a_2 + ... + a_n) / n ; ; ; Write a Scheme function AVERAGE that computes the average ; of the numbers in its input list. ; ; > (average '(1 2 3 2 9 6)) ; 23/6 ; > (average '(1 1 1 1)) ; 1 ; > (average '(2 3 4 3 2)) ; 14/5 ; > (average '()) ; 0 ; ; Use a higher-order function in your solution to this ; problem. ; ; Your function should avoid the possibility of dividing ; by zero - an arithmetic error. ; ; Contract: ; Header: ; Purpose: ; Pre-function tests: ; Function definition: ; Post-function printf's: (newline) (newline)