;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname practice_test_key) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) ; Q1 (1 point): What does the following statement evaluate to? (+ 3 (- 4 3)) ; Answer: 4 ; Q2 (2 points): Consider the following function signature, purpose, and header: ; two-x : number -> number ; returns two times the supplied number (define (two-x x) (...)) ; Complete the design recipe by writing the function body and one test ; Answer: (define (two-x-answer x) (* 2 x)) (check-expect (two-x-answer 5) 10) ; Q3 (2 points): Consider the following data definition: ; a cheese is a: ; - symbol name ; - number cost ; - symbol color (define-struct cheese (name cost color)) ; Write a template for this data definition ; Answer: ; (define (fun-for-cheese c) ; ( ... (cheese-name c) ... ; ... (cheese-cost c) ... ; ... (cheese-color c) ... )) ; Q4 (2 points): Consider the following template: ; (define (fun-for-food f) ; (cond [(cheese? f) (...)] ; [(bread? f) (...)] ; [(broccoli? f) (...)]) ; Write the data definition for "food" that corresponds to the template above. ; Assume data definitions and struct statements have already been supplied for ; bread and broccoli, similar to the for "cheese" above (the details of these ; definitions are unimportant for the "food" definition). (define-struct bread (name cost color)) (define-struct broccoli (name cost color)) ; Answer: ; a food is either a: ; - cheese, or ; - bread, or ; - broccoli ; Q5 (6 points): Consider the following data definition: ; a list-of-food is either: ; - empty ; - (cons food list-of-food) ; where food is defined as in your answer to Q4 above ; Use the design recipe to write a function "count-cheese," which counts ; how many foods in a list-of-food are cheeses. ; Guidelines: ; You don't need to explictly write out the template, but it may be helpful to do so ; You don't need to supply examples or tests, use the test cases given below. ; Answer: ; count-cheese : list-of-food -> number ; returns the number of foods in the list that are cheese (define (count-cheese lof) (cond [(empty? lof) 0] [else (cond [(cheese? (first lof)) (+ 1 (count-cheese (rest lof)))] [else (count-cheese (rest lof))])]) ) ;Test cases: (check-expect (count-cheese (list (make-cheese 'cheddar 12 'orange) (make-bread 'wheat 8 'brown) (make-cheese 'swiss 5 'white))) 2) (check-expect (count-cheese empty) 0) ; Q6 (2 points): Consider the following function: (define (f x) (cond [(> x 0) 7] [else (f x)])) ; a) what is the result of (f 4) ? ; Answer: 7 ; b) what is the result of (f -5) ? ; Answer: nothing -- infinite recursion