;; 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-intermediate-lambda-reader.ss" "lang")((modname practice_exam_key) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; Q1 (1 point): What does the following statement evaluate to? (+ 3 (- 4 3)) ; (+ 3 1) -> 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) (* 2 x) ) (check-expect (two-x 5) 10) ; Complete the design recipe by writing the function body and one test ; Q3 (2 points): Consider the following data definition: ; a cheese is a: ; - symbol ; - number ; - symbol (define-struct cheese (name cost color)) ; Write a template for this data definition ;(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) (fun-for-cheese f)] ; [(bread? f) (fun-for-bread f)] ; [(broccoli? f) (fun-for-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)) ; a food is either: ; - 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. ; count-cheese : list-of-food -> number ; returns the number of foods in list that are cheese (define (count-cheese lof) (cond [(empty? lof) 0] [else (+ (cond [(cheese? (first lof)) 1] [else 0]) (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) ? 7 ; b) what is the result of (f -5) ? infinite loop ; Q7 (2 points): Write a Racket expression (not a function -- no need to follow design ; recipe) that uses append and one of map, filter, and foldr to add duplicates of all ; odd elements in a list of numbers lon. So if lon is (list 1 4 3 6) your expression ; should evaluate to (list 1 4 3 6 1 3). (append lon (filter odd? lon)) ; Q8 (2 points): What is the signature of the following function? ;(define (f g x) (lambda (y) (+ (g y) 7))) ; f : (Y -> number) X -> (Y -> number) ; Q9 (2 points): Consider a number tree: ; a number-tree is either: ; - empty ; - (make-nt-node number[k] number-tree[left] number-tree[right]) ; What is an invariant that makes finding the max of the tree especially fast? How fast is it for a tree with n elements? How does this compare to a balanced BST with n elements? ; INVARIANT: k is larger than all numbers in left and right ; balanced BST takes about log_2 n, slower than constant for this num. tree