;;Practice exam: CMPU 101 Exam 1 Practice Exam Solutions ; ; Problem 1: ; ; Name three types of Racket entities that always evaluate to ; themselves and give examples of each of the types. ; ; ANSWER POSSIBILITIES: ; number: 1, string: "hi", quoted symbol: 'kitty, ; empty list: (), primitive function: +, character: #\a ; ; ; ; Problem 2: ; ; Write a predicate function called SMALL-VALUE? that takes in a ; data item (any valid Racket entity) as input and that returns ; #t if the input is a Boolean value or a number between 0 and ; 20 (including 0 and 20). For full credit, your solution ; should not use an if or a cond. If you have trouble writing ; a solution with no decision statement, try writing the ; function with a decision statement first and it might give ; you some insight into writing it without if or cond. ; ; Follow the design recipe and write a contract, header, and ; purpose. ; ; ; > (small-value? 7) ; #t ; ; > (small-value? #f) ; #t ; ; > (small-value? 'foo) ; #f ; ; Contract: (small-value? any-valid-racket-type) -> boolean ; Header: (define small-value? (lambda (any) ...)) ; Purpose: return #t if any is boolean or if any is a number and ; #f otherwise. ; ; Tests: (check-expect (small-value? 15) #t) (check-expect (small-value? 155) #f) (check-expect (small-value? #f) #t) ; Function: (define small-value? (lambda (any) (or (boolean? any) (and (number? any) (<= 0 any 20))))) ; ; Problem 3: ; ; Design a Scheme function that converts Euros to US ; Dollars. Since exchange rates change daily, you should ; make your code as readable and maintainable as possible ; by using a constant definition for the exchange rate. ; ; Be sure to follow the Design Recipe to get full credit for ; your solution. ; (define EXCH 1.2893);; find current exchange rate and make it a constant. ; Contract: (euro->dollar number) -> number ; Header: (define euro->dollar (lambda (euro) ...)) ; Purpose: Convert euro amount to equivalent dollar amount. ; Tests: (check-expect (euro->dollar 1000) 1289.3) (check-expect (euro->dollar 0) 0) ; Function: (define euro->dollar (lambda (euro) (* euro EXCH))) ; ; Problem 4(a): ; ; Evaluate the following programs (a and b) step by step (as ; if you were the Stepper), underlining the portion of the ; expression in each step to be evaluated in the next step, ; and separating each step with an arrow (->). Circle any ; parts of the functions that are not evaluated when the given ; function call is made. ; ; Function definition: ; ; (define (my-abs x) ; (cond ; [(> x 0) x] ; [(< x 0) (* -1 x)] ; [else 0])) ; ; Function call: ; (my-abs (- 4 10)) ; ; ANSWER: ; ; 1. The (- 4 10) evaluates to -6 ; 2. -6 is inserted into the cond expression everywhere x ; appears. ; 3. The first cond clause (> -6 0) is evaluated and returns #f ; 4. The second cond clause (< -6 0) is evaluated and returns ; #t ; 5. The body of the second clause (* -1 -6) is evaluated and ; the function returns 6. ; 6. The unevaluated parts of the cond are x in the first ; clause, and the entire else clause. ; ; 4(b): ; ; Function definition: ; ; (define (score x) ; (cond ; [(>= x 90) "A"] ; [(>= x 80) "B"] ; [(>= x 70) "C"] ; [(>= x 60) "D"] ; [else "F"])) ; ; Function call: ; (score (- 94 20)) ; ; ANSWER: ; ; 1. The (- 94 20) evaluates to 74 ; 2. 74 is inserted into the cond expression everywhere x ; appears. ; 3. The first cond clause (>= 74 90) is evaluated and returns ; #f ; 4. The second cond clause (>= 74 80) is evaluated and returns ; #f ; 5. The third cond clause (>= 74 70) is evaluated and returns ; #t ; 5. The body of the third cond clause "C" is evaluated ; and the function returns "C". ; 6. The unevaluated parts of the cond are the bodies of the ; first and second clauses and everything after the third ; clause. ; ; ; Problem 5: ; ; The expression (+ 8 2) evaluates to the value 10. It is a ; compound expression made up of three primitive values. For ; this problem, write five other Racket expressions whose ; values are also the number 10: ; ; * A single primitive value. 10 ; ; * Another compound expression made up of three primitive ; values. (+ 7 3) ; ; * A compound expression made up of four primitive values. ; (+ 4 4 2) ; ; * A compound expression made up of one primitive value ; and two compound subexpressions. ; (+ (+ 1 1) (+ 4 4)) ; ; * Any other kind of expression. (string->number "10") ; ; ; Problem 6: ; ; Describe what is wrong with the following function ; definitions: ; ; (a) (define (area sqr) ; (sqr sqr)) ; ; What would be returned? NOTHING--DEFINE ONLY HAS A ; SIDE-EFFECT,WRITING TO THE ; GLOBAL ENVIRONMENT. ; Would the definitions window run without errors if this ; expression were part of the code to be evaluated? YES ; Why or why not? BECAUSE A FUNCTION BODY IS EVALUATED ONLY ; WHEN THE FUNCTION IS CALLED (IF THE HEADER ; HAS NO ERRORS, LIKE THIS ONE. ; When would this definition cause an error (if ever)? ; IT WILL CAUSE AN ERROR BECAUSE THE SQR ; FUNCTION NAME HAS BEEN OVERSHADOWED BY ; DECLARING SQR AS A PARAMETER NAME. ; ERROR GIVEN BELOW: ; function call: expected a function after the ; open parenthesis, but received 4 ; ; ; (b) (define (f (+ 3 x) y) ; (* x y)) ; ; Would this definition cause an error? YES ; Why or why not? BECAUSE A PARAMETER LIST CAN'T ; CONTAIN AN EXPRESSION LIKE (+ 3 x). ; ; ; PROBLEM 7: ; ; Develop a function called smallest-of-3 that consumes three ; numbers and returns the smallest of them. ; ; Note: You are not allowed to use the built-in min function in ; your solution, use decision statements instead. ; ; Contract: (smallest-of-3 n1 n2 n3) -> number ; n1, n2, n3: numbers ; Purpose: Returns the smallest of n1, n2, and n3 ; Tests: (check-expect (smallest-of-3 3 2 1) 1) (check-expect (smallest-of-3 0 0 0) 0) (check-expect (smallest-of-3 -5 -23 0) -23) (check-expect (smallest-of-3 0 0 -2) -2) (check-expect (smallest-of-3 1 5 2) 1) (check-expect (smallest-of-3 7 2 5) 2) ; Function: (define (smallest-of-3 n1 n2 n3) (cond [(<= n1 n2 n3) n1] [(<= n1 n3 n2) n1] [(<= n2 n1 n3) n2] [(<= n2 n3 n1) n2] [(<= n3 n1 n2) n3] [else n3])) ; ; PROBLEM 8: ; ; Develop a function called smart-add that consumes 2 arguments ; and adds them. The trick is that the arguments may be either ; numbers (like 18) or strings of digits (like "18"). Your ; function should be able to handle both types for either input ; argument. ; ; HINT: There are 2 parameters in this function, each of which ; could be either of 2 types, so you'll need at least 4 test ; cases. ; ; Contract: (smart-add ns1 ns2) -> number ; ns1, ns2: number or string of digits ; Purpose: To add the numeric representation of 2 numbers ; Test: (check-expect (smart-add "4" "6") 10) (check-expect (smart-add 4 "6") 10) (check-expect (smart-add "4" 6) 10) (check-expect (smart-add 22 20) 42) ; Function: (define smart-add (lambda (ns1 ns2) (cond ;; both numbers [(and (number? ns1) (number? ns2)) (+ ns1 ns2)] ;; both strings [(and (string? ns1) (string? ns2)) (+ (string->number ns1)(string->number ns2))] ;; ns1 is number, so ns2 must be string [(number? ns1) (+ ns1 (string->number ns2))] ;; ns1 is number and ns2 is a string [else (+ (string->number ns1) ns2)]))) ; ; PROBLEM 9 (VERSION 1): ; ; Develop a function called roman-value that consumes 1 quoted ; symbol as an argument and produces a number that is the decimal ; version of the given Roman numeral. You should make sure your ; function works for roman numerals up to 1,000 ('i, 'v, 'x, 'l, ; 'c, 'd, and 'm). If any symbol other than one of the 7 Roman ; numerals is entered, you should return the quoted symboln'huh?. ; ; Contract: (roman-value s) -> (union number symbol) ; s: quoted symbol ; Purpose: to return given quoted symbol s in its numeric ; form or 'huh? if it is not 'i 'v 'x 'l 'c 'd or 'm. ; Tests: (check-expect (roman-value 'i) 1) (check-expect (roman-value 'v) 5) (check-expect (roman-value 'x) 10) (check-expect (roman-value 'l) 50) (check-expect (roman-value 'c) 100) (check-expect (roman-value 'd) 500) (check-expect (roman-value 'm) 1000) (check-expect (roman-value 'u) 'huh?) ; Function: (define roman-value (lambda (s) (cond ;; no pre-clause comments because it is apparent from problem [(symbol=? s 'i) 1] ;; what each clause should return. [(symbol=? s 'v) 5] [(symbol=? s 'x) 10] [(symbol=? s 'l) 50] [(symbol=? s 'c) 100] [(symbol=? s 'd) 500] [(symbol=? s 'm) 1000] [else 'huh?]))) ; ; PROBLEM 9 (VERSION 2): ; ; Rewrite the following function without using the if special ; form and without including the literal boolean values #t and ; #f in the definition: ; ; (define in-range? ; (lambda (x) ; (if (> x -3) ; (if (<= x 4) ; (if (> x 2) ; #t ; #f)) ; (if (< x -5) ; #f #t)))) ; ; ; ANSWER: ; (define in-range-v2? ; (lambda (x) ; (or (< -5 x -3) ; (and (> x 2) (<= x 4))))) ; ; ; PROBLEM 10: ; ; Use the method shown in class to convert the following infix ; expressions to prefix notation. Do not simplify or remove ; operators. ; ; (a) (x - 32) * (5/9) ; ; (* (- x 32) (/ 5 9)) ; ; ; (b) (x + 3y) * (x - y) ; ; (* (+ x (* 3 y)) (- x y)) ; ; ; (c) 4 + 3 * 7 - 5 / (3 + 4) + 6 ; ; Step 1: Expand all terms. Convert X to *. ; Step 2: Completely parenthesize: ; (((4 + (3 * 7)) (5 / (3 + 4))) + 6) ; Step 3: Move all operators to right of nearest ; enclosing left parenthesis: ; (+ (- (+ 4 (* 3 7)) (/ 5 (+ 3 4 ))) 6) ; ; ; ; PROBLEM 11: ; ; Show the global environment entries created by the following 5 ; definitions when they are executed in DrRacket: ; ; (define U 35) ; (define V 17) ; (define W (* U V)) ; (define X (+ V W)) ; (define f (lambda (a b) (+ a (* b 2)))) ; ; ; Show the result of typing the name of each entry in the ; Interactions window. ; ; YOU CAN TRY THIS OUT IN DRRACKET TO SEE THE SOLUTION. ; ; ; ; PROBLEM 12: ; ; Given particular character sequences (words),indicate ; whether the word is a special form, a built-in function, ; or neither. ; ; If you answer special form, indicate how the form is ; evaluated and give an example of a valid use of the form, ; including the return value (or void if a side-effect ; is the only thing returned). ; ; If you answer built-in function, give an example of invoking ; the function on valid arguments and indicate the output for ; your example. ; ; If you think the given word is an undefined name, type ; "undefined". ; ; (a) typo UNDEFINED ; ; ; (b) cond SPECIAL FORM (cond ; [bool1 result1] ; [bool2 result2] ; ... ; [else result]) ; DECISION STATEMENT. ONLY 1 CLAUSE IS EVER EVALUATED. ; ; ; (c) string-append BUILT-IN FUNCTION. CONSUMES STRINGS AND ; JOINS THEM TOGETHER. ; ; ; (d) my-func UNDEFINED ; ; ; (e) begin SPECIAL FORM. ALLOWS MORE THAN ONE STATEMENT ; TO BE WRITTEN IN BODY OF FUNCTION (USUALLY PRINTF ; STATEMENTS) ; ;