CS115F2008M2

CS115F2008M2 - UWaterloo CS115 Midterm_2 2008 Fall 2009...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UWaterloo CS115 Midterm_2 2008 Fall 2009 Oda_Eagle Production CS115/116/125/134/135/136/241/240/341/343 Tutor odaeagle@hotmail.com The following notes are relevant for all questions: • We use the term “Scheme” as a short form for “Beginning Student Scheme with List Abbreviations” • 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. 招收 CS 大三課程以下学生有意者(具體請見詳表) + odaeagle@hotmail.com CELL: 519-729-1582 已经教过 100+的学生 ◦ 经验丰富,童叟无欺,价格合理,内容充实,简单易懂 具體詳細表 CS115 作業答疑,考試複習,有過去考試的卷子和過去所有作業準確答案,也有自出的題,價格面儀 CS116/136 普通作業答疑不收費,如果一對一輔導價格面議 CS134 擁有最全的資料,無論作業還是近四個 term 所有考試卷子(不包括 final),價格面儀 CS240 答疑解惑,不收費 CS241 答疑+Debug 需要請請吃飯 CS246 答疑解惑 免費 ,Debug 需要請吃飯 CS245/251 愛莫能助 CS341 答疑解惑 不收費 CS350 答疑解惑 + Debug 不收費 CS343 答疑解惑 + Debug 不收費 CS360 跟我討論問題,我請你吃飯 其他課程諸如 Math135/136/137/138,可以給你介紹好老師 想學 Flash 的同學也可以找我,不過價格會比 CS 課貴 想學唱歌的別找我,謝謝 喝酒的找我,隨時奉陪 1. Suppose you want to represent information about books, title, author, number of pages, and binding( hardcover or paperback ). ( a ) Give a structure definition for structure book. Choose appropriate field names. ( b ) Give a data definition for the structure book. Choose appropriate types for each field. ( c ) Give a recursive data definition for book-list, a list of books ( d ) Combine the list and structure templates to complete the template for a function my-booklist-fun with the contract shown below: ;;my-book-list-fun: book-list → any 2. Assume the following definition has been run in DrScheme's Definition window ( define (modify nbr ) ( cond [ ( even? nbr ) ( modify ( / nbr 2 ) ) ] [ else ( modify ( - nbr 1 ) ) ] ) ) ( modify 4 ) Briefly explain what happens when ( modify 4 ) is run in the Interaction window. A condensed trace is one appropriate way to answer this question. 3. In class, we showed the following two visualization of the sample list ( cons 'blue ( cons true empty ) ) 'blue 'blue True empty True Using either of these visualization, draw a representation of the following list ( cons 'red ( cons ( cons 4 ( cons 5 empty ) ) ( cons true empty ) ) ) 4. The subquestions below refer to the following function ( define ( a-fun n ) ( cond [ ( and ( >= 50 n ) ( > n 10 ) ) 0 ] [ ( or ( > n 60 ) ( > 20 n ) ) 1 ] ) ) ( a ) In the chart below, for each possible outcome of the function application ( a-fun n ), indicate the ranges of integers n that would result in that outcome Outcome Range 0 1 error ( b ) Trace the function application ( a-fun 70 ). Show each step. 5. The function my-fun consumes a number nbr and produces either a string or a number, as follows: 3 • ( my-fun nbr ) is if nbr is an integer not equal to 2 nbr −2 • ( my-fun nbr ) is “Bad choice” if nbr is 2; and • ( my-fun nbr ) is “Non-Integer” if nbr is not an iteger ( a ) Provide a contract for my-fun ( b ) Create a Scheme program my-fun. Organize your code so that it is simple and easy to understand. You may wish to use the Scheme predicate integer? 6. Recall the structure timestruct: ;; A timestruct is a structure ( make-timestruct hour mins ) where hours is an integer ;; in the range 0 to 23 and mins is an integer in the range 0 to 59 ( define-struct timestruct ( h m ) ) ( a ) Define a constant dinner-time, of type timestruct, for the time 18:00. ( b ) Create a Scheme function before? That consumes two timestructs on the same day and produces true if time1 is before time2 and false otherwise. ( c ) Create a Scheme function later-time that consumes a timestruct current and a positive integer hours-later and produces a timestruct representing the time hours-later hours after current. You can assume that hours-later hours after current will be on the same day as current. 7. The functions below make use of the following data definitions. You may find the following Scheme predicates useful: number? Symbol? And string? ;;A mixed-item is a number, a symbol, or a string ;;A mixed-list is either empty or is of the form ( cons mi ml ), where ;;mi is a mixed item and ml is mixed-list ( a ) Create a Scheme function number-of-symbols that consumes a mixed list and produces the number of symbols in the list. For example, the function application ( number-of-symbols ( list 'red 786 “string” 'blue “a cat” ) ) --> 2 ( b ) Create a Scheme function convert that consumes a mixed list and produces a list of numbers, formed by replacing each symbol by 0 and each string by 1. For example, ( convert ( list 'red 786 “string” 'blue “a cat) ) → ( list 0 786 1 0 1 ). ( c ) Give a contract for the Scheme function strings-only, which consumes a mixed-list and produces a list of strings, formed by keeping only the items that are strings. For example, ( strings-only( list 'red 786 “string” 'blue “a cat” ) ) produces ( list “string” “a cat” ). ( d ) Create a Scheme function strings-only as defined in part ( c ). ...
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.

Ask a homework question - tutors are online