{\rtf1\ansi\ansicpg1252\uc1 \deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman{\*\falt Thorndale};}{\f2\fmodern\fcharset0\fprq1{\*\panose 02070309020205020404}Courier New;}
{\f44\froman\fcharset238\fprq2 Times New Roman CE{\*\falt Thorndale};}{\f45\froman\fcharset204\fprq2 Times New Roman Cyr{\*\falt Thorndale};}{\f47\froman\fcharset161\fprq2 Times New Roman Greek{\*\falt Thorndale};}
{\f48\froman\fcharset162\fprq2 Times New Roman Tur{\*\falt Thorndale};}{\f49\froman\fcharset177\fprq2 Times New Roman (Hebrew){\*\falt Thorndale};}{\f50\froman\fcharset178\fprq2 Times New Roman (Arabic){\*\falt Thorndale};}
{\f51\froman\fcharset186\fprq2 Times New Roman Baltic{\*\falt Thorndale};}{\f60\fmodern\fcharset238\fprq1 Courier New CE;}{\f61\fmodern\fcharset204\fprq1 Courier New Cyr;}{\f63\fmodern\fcharset161\fprq1 Courier New Greek;}
{\f64\fmodern\fcharset162\fprq1 Courier New Tur;}{\f65\fmodern\fcharset177\fprq1 Courier New (Hebrew);}{\f66\fmodern\fcharset178\fprq1 Courier New (Arabic);}{\f67\fmodern\fcharset186\fprq1 Courier New Baltic;}}{\colortbl;\red0\green0\blue0;
\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;
\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;}{\stylesheet{\ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \snext0 Normal;}
{\*\cs10 \additive Default Paragraph Font;}{\s15\ql \li0\ri0\nowidctlpar\aspalpha\faauto\rin0\lin0\itap0 \fs24\cf1\lang1033\langfe255\cgrid\langnp1033\langfenp255 \snext15 Default;}{\s16\ql \li0\ri0\nowidctlpar
\tqc\tx4320\tqr\tx8640\aspalpha\faauto\rin0\lin0\itap0 \fs24\cf1\lang1033\langfe255\cgrid\langnp1033\langfenp255 \sbasedon15 \snext16 header;}{\*\cs17 \additive \fs24\cf1\lang0\langfe255\langnp0\langfenp255 Numbering Symbols;}}{\info
{\title CS 131 - Intro to Computer Science I - Fall 2002}{\author Sharon Tuttle}{\operator CNRS}{\creatim\yr2002\mo12\dy6\hr23\min42}{\revtim\yr2002\mo12\dy6\hr23\min55}{\printim\yr2002\mo12\dy6\hr23\min34}{\version9}{\edmins3}{\nofpages3}{\nofwords1363}
{\nofchars7774}{\*\company CNRS-HSU}{\nofcharsws0}{\vern8269}}\margt2079 \deftab1250\widowctrl\ftnbj\aenddoc\noxlattoyen\expshrtn\noultrlspc\dntblnsbdb\nospaceforul\hyphcaps0\horzdoc\dghspace120\dgvspace120\dghorigin1701\dgvorigin1984\dghshow0\dgvshow3
\jcompress\viewkind1\viewscale100\nolnhtadjtbl \fet0\sectd \sbknone\linex0\headery1440\sectdefaultcl {\header \pard\plain \s16\ql \li0\ri0\nowidctlpar\tqc\tx4320\tqr\tx8640\aspalpha\faauto\rin0\lin0\itap0 
\fs24\cf1\lang1033\langfe255\cgrid\langnp1033\langfenp255 {\fs16 CS 131 - Homework #12\tab \tab p. }{\field{\*\fldinst {\fs16 \\page}}{\fldrslt {\fs16\lang1024\langfe1024\noproof 1}}}{
\par }{\fs16 Fall 2002
\par }}{\*\pnseclvl1\pnucrm\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl2\pnucltr\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl3\pndec\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang{\pntxta )}}
{\*\pnseclvl5\pndec\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl6\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl8
\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}\pard\plain \s15\qc \li0\ri0\nowidctlpar\aspalpha\faauto\rin0\lin0\itap0 
\fs24\cf1\lang1033\langfe255\cgrid\langnp1033\langfenp255 {\b\fs20 CS 131 - Intro to Computer Science I - Fall 2002
\par Homework #12
\par DUE: Friday, December 13th, by }{\b\fs20\ul 5:00 pm}{\b\fs20 
\par }{\b\fs20\ul 
\par }\pard \s15\ql \li0\ri0\nowidctlpar\aspalpha\faauto\rin0\lin0\itap0 {\fs20 As always, you will turn in certain function definitions and expressions that you have typed into the DrScheme definitions window and saved into one or more file names ending in }{
\b\fs20 .scm}{\fs20  . In fact, this time you will send in }{\b\fs20 THREE files }{\fs20 --- problems 1 and 2 need to be in separate files (because of GUI's).
\par 
\par The same homework and style guidelines apply as before, }{\b\fs20 except}{\fs20  as modified below. (Yes, we'll keep the same program recipe, but with modifications noted below.)
\par 
\par PLEASE NOTE: there are all sorts of lovely type-conversion functions in DrScheme --- }{\b\fs20 number->string}{\fs20 , }{\b\fs20 string->number}{\fs20 , }{\b\fs20 symbol->string}{\fs20 , }{\b\fs20 string->symbol}{\fs20 ...
\par }{\b\fs20 
\par }\pard \s15\ql \fi-420\li420\ri0\nowidctlpar\aspalpha\faauto\rin0\lin420\itap0 {\b\fs20 0.}{\fs20 \tab Study the two posted in-class examples, read Section 26, and ask me if you have any questions about the purpose or basic idea of a }{\b\fs20 
termination argument}{\fs20  for a function using generative recursion.}{\b\fs20 
\par 
\par }\pard \s15\ql \fi-435\li420\ri0\nowidctlpar\aspalpha\faauto\rin0\lin420\itap0 {\b\fs20 1.\tab }{\fs20 (this problem is worth}{\b\fs20  40}{\fs20  points in all; turn it in in an e-mail with Subject: }{\b\fs20 131 HW 12 #1}{\fs20 )}{\b\fs20 
\par \tab PART 1: }{\fs20 (Note: the following should be quite similar to things we have done earlier; I think there are some sl
ight variations, however.) The scenario is a toy store. Each toy has a name (a symbol), a price (a number WITH NO FRACTIONAL PART --- to see why, try the following command in DrScheme:
\par }{\b\fs20 \tab \tab }{\f2\fs20 (number->string 3.98)\tab ;; YUCK!!!}{\fs20 
\par }{\b\fs20 \tab }{\fs20 ), and a quantity (a natural number). You'd like to represent all of the toys in the store. Write appropriate data definitions (please make use of a certain hopefully-getting-very-familiar parametric data definition, also...) 

\par }{\b\fs20 
\par \tab }{\fs20 Consider how you would write the following two functions: (you will not turn  these in...)
\par }\pard \s15\ql \fi-435\li855\ri0\nowidctlpar\aspalpha\faauto\rin0\lin855\itap0 {\fs20 *\tab }{\b\fs20 get-price}{\fs20  - takes a list of toys and the name of a toy, and returns the price of that toy; it should return }{\b\fs20 false}{\fs20 
 if there is no toy with that name.}{\b\fs20 
\par *\tab get-quantity }{\fs20 - which takes a list of toys and the name of a toy, and returns the quantity of that toy; it should return false if there is no toy with that name.
\par }{\b\fs20 
\par }{\fs20 Hey, but look how similar these are! So, write }{\b\fs20 get-toy-field}{\fs20  that takes a list of toys, the name of a toy, and the }{\b\fs20 selector function}{\fs20  for the desired toy field, and retu
rns the value of that field for the toy with that name. It should return }{\b\fs20 false}{\fs20  if there is no toy with that name in the list.}{\b\fs20 
\par }\pard \s15\ql \fi15\li420\ri0\nowidctlpar\aspalpha\faauto\rin0\lin420\itap0 {\b\fs20 
\par }\pard \s15\ql \fi-435\li855\ri0\nowidctlpar\aspalpha\faauto\rin0\lin855\itap0 {\b\fs20 PART 2: }{\fs20 Make a GUI, using the teachpack gui.ss. You get a CHOICE, however. You can}{\b\fs20  EITHER:
\par }\pard \s15\ql \fi-435\li1290\ri0\nowidctlpar\aspalpha\faauto\rin0\lin1290\itap0 {\fs20 *\tab 
build a GUI to let a person enter a toy's name into a textfield, and when an appropriately-labelled button is pushed, show that toy's PRICE in a message field, OR
\par 
\par *\tab build a GUI to let a person enter a toy's name into a textfield, and when an appropriately-labelled button is pushed, show that toy's QUANTITY in a message field.
\par }{\b\fs20 
\par }{\fs20 (in either case --- if the user chooses a toy that isn't in the list, that message field will show a message saying that the toy is not available, rather than generating an error.)
\par }{\b\fs20 
\par *\tab for EXTRA CREDIT:
\par }\pard \s15\ql \fi-435\li1710\ri0\nowidctlpar\aspalpha\faauto\rin0\lin1710\itap0 {\b\fs20 +3: \tab }{\fs20 instead of a textfield, fill a }{\b\fs20 choice menu}{\fs20 
 with the possible toy names, so that users can select a toy name instead of having to type it in; (note: the toy-not-available message will not be necessary here, will it?)
\par }{\b\fs20 
\par +3: \tab }{\fs20 create a GUI with two buttons, appropriately labelled: pushing one causes the current toy's (whether in textfield or choice menu) PRICE to be displayed. Pushing the other button causes its QUANTITY to be displayed. 
\par }\pard \s15\ql \li0\ri0\nowidctlpar\aspalpha\faauto\rin0\lin0\itap0 {\b\fs20 
\par }\pard \s15\ql \fi-435\li420\ri0\nowidctlpar\aspalpha\faauto\rin0\lin420\itap0 {\b\fs20 2.\tab }{\fs20 (this problem is worth}{\b\fs20  30 }{\fs20 points in all; turn it in in an e-mail with Subject:}{\b\fs20  131 HW 12 #2)
\par \tab WARNING: }{\fs20 this problem does involve vectors, which we will at least introduce on}{\b\fs20  Tuesday, December }{\fs20 10th (and, of course, see the assigned reading, pp. 426-438). The vector-related aspects of this problem are
 quite straightforward, but they will provide you with some vector practice.
\par }{\b\fs20 
\par \tab }{\fs20 Note that a vector's purpose in life is to allow one to directly access any element within it, without looking at all of the elements before it. You can grab any element by simp
ly specifying its position, also known as its index, where the position/index of the 1st element is 0,of the 2nd element is 1,etc.
\par }{\b\fs20 
\par \tab So: \tab (vector val0 val1 ... valn) }{\fs20 creates a vector of the (n+1) values val0 through valn,
\par }{\b\fs20 
\par \tab \tab (vector-ref (vector val0 val1 ... valn) i)}{\fs20  returns the element in the given vector at \tab         position/index }{\b\fs20 i}{\fs20 , or }{\b\fs20 vali 
\par 
\par \tab \tab (vector-length (vector val0 val1 ... valn)) }{\fs20 returns the length of the vector (which is \tab        (n+1), note)}{\b\fs20 
\par 
\par \tab \tab vector? }{\fs20 is a predicate that returns true if its argument is a vector, and false otherwise.
\par }{\b\fs20 
\par \tab \tab }{\fs20 (note that you get an error if you call vector-ref with a position/index <0 and 
\par \tab \tab         >(vector length -1))
\par }{\b\fs20 
\par \tab \tab (build-vector N f}{\fs20 ) is a lot like build-list, except that it builds a vector instead of a list, \tab         where the first element if (f   0), the second is (f   1) ... and the last is 
\par \tab \tab         (f   (sub1 N))
\par }{\b\fs20 
\par \tab }{\fs20 Oh yes, and here's a parameterized data definition for a vector whose elements are of some type:
\par 
\par \tab }{\f2\fs20 ;;-----------------------------------------------------------------}{\fs20 
\par }{\fs20\loch\af2 \tab \hich\af0\dbch\af0\loch\f2 ;; a (vectorof ITEM) is
\par \tab \hich\af0\dbch\af0\loch\f2 ;;    (vector v-0 v-1 ... v-n)
\par \tab \hich\af0\dbch\af0\loch\f2 ;; ...where v-0, v-1, ... v-n are all of type ITEM.
\par \tab \hich\af0\dbch\af0\loch\f2 ;;-----------------------------------------------------------------
\par }{\fs20 \tab \tab 
\par \tab Now, on
 to the problem! Consider the following scenario: a set of apartments are numbered (conveniently enough) 0 to 29. This apartment complex has one of those security gates where one has to dial the apartment's phone code and get the occupants to dial a speci
al number to get the security gate to open. 
\par }{\b\fs20 
\par \tab PART 1: }{\fs20 Write a function}{\b\fs20  generate-phone-code. }{\fs20 This takes a single argument --- which it ignores! --- and simply generates a random number between 1000 and 9999, inclusive. (Think: how can you make sure that the
 result is >= 1000 and <= 9999? Remember to use named constants as appropriate!)
\par }\pard \s15\ql \fi-435\li855\ri0\nowidctlpar\aspalpha\faauto\rin0\lin855\itap0 {\fs20 \tab (ok, so why do I want you to have one argument that is ignored? you'll see below...)
\par }{\b\fs20 
\par }\pard \s15\ql \fi-435\li420\ri0\nowidctlpar\aspalpha\faauto\rin0\lin420\itap0 {\b\fs20 \tab PART 2: }{\fs20 We now want to set up a vector to contain the apartments' phone codes. Write a function}{\b\fs20  set-phone-codes }{\fs20 
that takes the desired size of the resulting vector as input, and creates a vector of that size where each element is randomly assigned a 4-digit phone code between 1000-9999. (Hint: use generate-phone-code and build-vector; we can
 get away with this because of the ignored argument in generate-phone-code... 8-) )
\par }{\b\fs20 
\par \tab PART 3: }{\fs20 Now, write a function}{\b\fs20  get-phone-}{\fs20 code that accepts a vector of phone codes and an apartment number (always assumed to be between 0 and (vector size - 1)!), and ret
urns the phone code for that apartment (that is, the phone code at that index). Have it return false if given an index not within the bounds for that vector (if the index is <0 or >=vector size).
\par }{\b\fs20 
\par \tab PART 4}{\fs20 : Finally, define and "fill" a phone code vector for
 the apartment complex of 30 apartments. And, build a GUI that lets a user enter in the desired apartment number, and displays the phone code for the specified apartment. (I'll leave the rest of the GUI design choices to you, except for this: if a user is
 able to enter an "illegal" apartment number, display a message saying that there's no such apartment, rather than generating an error or halting the GUI.)
\par }\pard \s15\ql \fi-435\li855\ri0\nowidctlpar\aspalpha\faauto\rin0\lin855\itap0 {\b\fs20 \tab 
\par }\pard \s15\ql \fi-435\li420\ri0\nowidctlpar\aspalpha\faauto\rin0\lin420\itap0 {\b\fs20 3.\tab }{\fs20 (worth}{\b\fs20  15 }{\fs20 points in all; it and #4 can be turned in in an e-mail with Subject:}{\b\fs20  131 HW 12 the rest)
\par \tab WARNING}{\fs20 : this problem is quite simple, but it does involve using set! and begin, which we will at least introduce on }{\b\fs20 Tuesday, December }{\fs20 
10th (and, of course, see the assigned reading, Sections 34-35). It will at least provide some simple practice using these features.
\par }{\b\fs20 
\par \tab }{\fs20 Two contract-related notes: if a function takes no input, just leave the left-side of its arrow blank. That is, \tab \tab \tab }{\f2\fs20 ;; CONTRACT : myfun1 : -> number}{\fs20 
\par }{\fs20\loch\af2 \tab }{\fs20 ...is the contract for a function myfun1 that takes no argument and returns a number.}{\fs20\loch\af2 
\par }{\b\fs20 
\par \tab }{\fs20 But, if a function returns nothing, we use the special term void for that on the right-hand-side of the arrow, in its contract. That is,\tab }{\f2\fs20 ;; CONTRACT: myfun2: string -> void}{\fs20 
\par }{\fs20\loch\af2 \tab }{\fs20 ...is the contract for a function myfun2 that takes a string and returns nothing (it only has an effect).}{\fs20\loch\af2 
\par }\pard \s15\ql \li0\ri0\nowidctlpar\aspalpha\faauto\rin0\lin0\itap0 {\b\fs20 
\par }\pard \s15\ql \fi-435\li420\ri0\nowidctlpar\aspalpha\faauto\rin0\lin420\itap0 {\b\fs20 \tab }{\fs20 (from CSC 160 HW #9, S. Bloch, Adelphi University)
\par \tab Develop a cooperating pair of functions named }{\b\fs20 count-calls }{\fs20 and}{\b\fs20  reset. }{\fs20 Each function takes no arguments (which means you need to be in Advanced Student mode to write them). }{\b\fs20 reset}{\fs20 
 returns nothing at all, while }{\b\fs20 count-calls}{\fs20  returns how many times it has been called since the last time }{\b\fs20 reset}{\fs20  was called. (Such a pair of functions might be used, for example, in a hit counter for a Web page.) 
\par \tab      Hint: Use a global variable, which both functions can modify, to keep track of the count. 
\par }{\b\fs20 
\par 4. \tab }{\fs20 (worth}{\b\fs20  15 points }{\fs20 in all)}{\b\fs20  }{\fs20 This is slightly more interesting, but still quite do-able.
\par }{\b\fs20 \tab }{\fs20 (from CSC 160 HW #9, S. Bloch, Adelphi University)
\par \tab Develop a cooperating pair of functions named}{\b\fs20  init }{\fs20 and}{\b\fs20  next}{\fs20 . }{\b\fs20 init }{\fs20 takes in a list, and returns nothing. next takes in nothing, and returns the "next" element of the list that was given to}{
\b\fs20  init}{\fs20 . For example, }{\b\fs20 
\par 
\par }{\fs20           (init (list 'a 'b 'c))) 
\par           (next) 
\par           'a 
\par           (next) 
\par           'b 
\par           (next) 
\par           'c 
\par 
\par      \tab What do you think}{\b\fs20  next }{\fs20 should do if it is called a fourth time? Make a conscious decision, document it (include it in}{\b\fs20  next}{\fs20 's}{\b\fs20  }{\fs20 
purpose statement), and include a commented-out test of this situation.
\par }}