This preview shows page 1. Sign up to view the full content.
Unformatted text preview: UWaterloo CS115 Midterm_1 2009 Winter
2009 Oda_Eagle Production
The following notes are relevant for all questions:
• We use the term “Scheme” as a short form for “Beginning Student Scheme with List
• Unless we ask specifically for another part of the design recipe, the instructions “Create a
Scheme function” means that you are to give the definition only.
• We use arithmetic functions as well as following built-in functions, described briefly here for
◦ max determines the largest of two or more numbers
◦ min determines the smallest of two or more numbers
◦ sqrt determines the positive square root of a number
◦ string-length determines the length of a string
◦ string-append combines two or more strings into a single string, in the order given
◦ check-expect determines if two values are exactly the same, and is to be used for tests when
they are requested
招收 CS 大三課程以下学生有意者（具體請見詳表）
已经教过 100+的学生 ◦ 经验丰富，童叟无欺，价格合理，内容充实，简单易懂
CS134 擁有最全的資料，無論作業還是近四個 term 所有考試卷子（不包括 final），價格面儀
CS241 答疑+Debug 需要請請吃飯
CS246 答疑解惑 免費 ，Debug 需要請吃飯
CS341 答疑解惑 不收費
CS350 答疑解惑 + Debug 不收費
CS343 答疑解惑 + Debug 不收費
想學 Flash 的同學也可以找我，不過價格會比 CS 課貴
喝酒的找我，隨時奉陪 ◦ Question 1. [ 7 marks ] Scheme Errors
for each row in the table below, determine what would happen if you opened DrScheme and tried to
evaluate the given code. If there is an error, briefly explain how Scheme's Syntax rules are violated or
describe what goes wrong when the code is run. If there is no error, give the value produced by
DrScheme. The first two rows have been completed for you.
Scheme Code Explanation/Value (+12) 3 (+12)) Error. There is an extra ). (+111)
( sqrt ( + 25 ( / 5 0 ) ) )
( string-length 9 )
( - ( + ( max 3 4 ) ( min 1 2 ) ) 1 )
( define ( f ) 42 ) Question 2. [ 5 marks ] A Basic Scheme Function
Write the Scheme definition of the function g( x , y ) = ( x + y )2. for this question, you do not need to
follow the steps of the design recipe - only the function definition required. Question 3. [ 5 marks ] Tracing
Trace the following Scheme program: ( + ( - ( * 2 5 ) ( min 5 2 ) ) 2 ). Put one step on each line. You
may not need all the lines. Question 4. [ 8 marks ] Partial Design Recipe
Suppose you wish to write a Scheme predicate mixed-chars? That consumes a string and produces true
if that string contains both alphabetical ( a,B,c etc. ) and numerical characters ( 1,2,3 etc ), and
produces false otherwise. In the spaces below, write the contract, purpose, one example and header for
the predicate mixed-chars?
Do not write the function body.
Header: Questions 5. [ 2 marks ] Testing
The function create-userid produces a string corresponding to a new userid derived from an individual's
first and last names.
( define ( create-userid first-name last-name )
( string-append first-name “.” last-name “@gmail.com” ) )
write a complete test for create-userid. Question 6. [ 3 marks ] Tracing a Conditional Expression
Answer the following questions about the function choose.
( define ( choose num )
[ ( and ( <= 10 num ) ( <= num 19 ) ) “path 1” ]
[ ( or ( < num 0 ) ( > num 15 ) ) “path 2”]
[ else “path 3” ] ) )
What value is produced by ( choose 18 )? What value is produced by ( choose 12 )?
Give a single value for n in ( choose n ) which produces the value
“path 3” Question 7. [ 10 marks ] A complete Scheme Function with Design Recipe
Using all the steps of the design recipe. Create a Scheme function engraving-cost that consumes a
positive number per-char-cost and a non-empty string message, and produces the cost of engraving
message on a MP3 player. The total cost for engraving is the number of characters( including spaces )
in message times the per-char-cost, plus an engraving fee of $4.95.
All of the steps of the design recipe are important: only a small number of marks will be alloted to the
correctness of the definition of engraving cost.
In addition, you should define any useful constants. ...
View Full Document
This note was uploaded on 10/24/2009 for the course CS 115 taught by Professor Graham during the Fall '08 term at Waterloo.
- Fall '08