Computer Since
Important notes
· Please note that only SOME, not all, of this homework's problems involve lists.
· NOTE: it is usually fine and often encouraged if you would like to write one or more helper functions to help you write a homework problem's required functions.
· HOWEVER -- whenever you do so, EACH function you define SHOULD follow all of the design recipe steps: first write its signature, then its purpose statement, then its function header, then its tests/check- expressions, then replace its ... body with an appropriate expression)
· Remember: Signatures and purpose statements are ONLY required for function definitions -- you do NOT write them for named constants or for non-function-definition compound expressions.
· You are expected to follow the Design Recipe for all functions that you design/define. So, each function is expected to include:
· a signature comment, including a nicely-descriptive name of the function, the types of expressions it expects, and the type of expression it produces. This should be written as discussed in class (and you can find examples in the posted in-class examples). For example,
; signature: rect-area: number number -> number
· a purpose statement comment, describing what the function expects and describing what it returns. For example,
; purpose: expects the length and width of a rectangle,
; and returns the area of that rectangle
· [following the design recipe, you will be writing the function header next; note that you don't need to write it twice. Follow the function header with a body of ... at this stage, and replace that ... with its body later, at the appropriate step in the design recipe.]
· check-expect (or check-within, or other check- operation) expressions expressing the specific examples that you write BEFORE writing your function body. (These may be placed before or after your actual function, but you are expected to create these BEFORE writing the function body. I'll have no way of knowing if you really write these in the correct order, but note that I won't answer questions about your function body without seeing your examples written as check- expressions first...) For example,
(check-expect (rect-area 3 4)
12)
(check-expect (rect-area 10 5)
50)
· How many check- expressions should you have? Remember, the basic rules of thumb are:
* you need a test/check- expression for each "case" or category of data that may occur, AS WELL AS one for each "boundary" if intervals are involved, and you can always add more if you'd like!
* if there is only one category of data, you should have at least two tests/check- expressions, for more-robust error-checking of your function.
* IFyou have a function involving random in such a way that one or more of the needed check-expressions does not seem possible, for each such needed test, put a string DESCRIBING what a specific example call should do, and follow it by that example call (so its result will appear under this string in the Interactions window when this is run).
For example:
"======================================"
"I expect to see a scene with a red circle, a green star,"
" and a purple star in random locations"
"======================================"
(draw-images-randomly
(cons (circle 30 "solid" "red")
(cons (star 40 "solid" "green")
(cons (star 50 "solid" "purple")
empty))))
* OR, you can put the failing check- expression, especially if looking at the not-matching actual and expected values nevertheless lets you tell if, except for the random part, your function really did work;
· [and, of course, your function definition itself!]
· You may include as many additional calls or tests of your function as you would like after its definition.
· You should use blank lines to separate your answers for the different parts of the homework problems. If you would like to add comments noting which part each answer is for, that is fine, too!
· Because the design recipe is so important, you will receive significant credit for the signature, purpose, header, and tests/check-expects portions of your functions. Typically you'll get at least half-credit for a correct signature, purpose, header, and examples/check-expects, even if your function body is not correct (and, you'll typically lose at least half-credit if you omit these or do them poorly, even if your function body is correct).
Lab Week 5 – turn in as part of this assignment lab for week #5.
Problem 1
Start up DrRacket, (if necessary) setting the language to How To Design Programs - Beginning Student level, and adding the HTDP/2e versions of the image and universe teachpacks by putting these lines at the beginning of your Definitions window:
(require 2htdp/image)
(require 2htdp/universe)
Put a blank line, and then type in:
· a comment-line containing your name,
· followed by a comment-line containing CS 111 - HW 5,
· followed by a comment-line giving the date you last modified this homework,
· followed by a comment-line with no other text in it --- for example:
; type in YOUR name
; CS 111 - HW 5
; last modified: 2016-02-22
;
Below this, after a blank line, now type the comment lines:
;
; Problem 1
;
The main purpose of this problem is to provide you with some practice writing expressions involving lists. You are NOT writing any new functions for this problem!
1 part a
· Paste or type in the comment containing the data definition for a Racket list.
;========== ; data definition: ; a list is: ; - empty, OR ; - (cons Anything list)
· Paste or type in the comment containing the TEMPLATE for a function that "walks through" a list ;=========================================== ; THIS is the FINISHED template for a ; function that "walks through" a list: ; ;(define (a-funct a-list …) ; (cond ; [(empty? a-list) ...] ; [else (... (first a-list) … ; (a-funct (rest a-list) …))] ; ) ;)
· Decide on a theme/topic, and define a named constant, with an appropriate, descriptive name, whose value is a list of at least FOUR things related to that theme/topic. Use cons to create your constant list.
1 part b
Now, USING your named constant list:
· Write an expression whose value is the first thing in your named constant list.
· Write an expression whose value is the list of all BUT the first thing in your named constant list.
· Write an expression whose value is JUST the SECOND thing in your named constant list.
· Write an expression whose value is JUST the THIRD thing in your named constant list.
1 part c
Note that Racket does have a built-in length function for lists -- (we designed len because it is such a good first example of a function that "walks through" a list. ):
; signature: length: list -> number
; purpose: expects a list, and produces the number of (top-level) ; elements in that list
(check-expect (length (cons 1 (cons 8 (cons 27 empty)))) 3)
USING your named constant list:
· Write an expression whose value is the length of your named constant list.
· Write an expression whose value is the length of the list of all BUT the first thing in your named constant list.
Problem 2
Next, in your definitions window, type the comment lines:
;
; Problem 2
;
Not all functions involving lists are necessarily recursive -- (usually they are recursive if you need to "visit" each list element or "walk through" the whole list).
Consider: what if, as part of a problem, you find yourself frequently wanting to grab JUST the third element in a given list? You might decide to write a helper function that expects a list, and returns the 3rd element in that list.
What should happen if some misinformed user calls it with a list of fewer than 3 elements? It is decided that, for this function's particular purposes, that this function should simply return #false if called with a list containing fewer than 3 elements.
Using the design recipe, design and write this function.
Problem 3
Next, in your definitions window, type the comment lines:
;
; Problem 3
;
3 part a
We want to work with some lists of numbers. So, paste or type in the data definition comment for a NumberList, and then paste or type in the comment containing the TEMPLATE for a function that "walks through" a NumberList.
3 part b
Following the design recipe, design and write a function that expects a list of numbers, and just returns the sum of the numbers in that list. That is, if this function has as its argument a list of numbers containing 7, 10, and 47, then the value produced would be 64. (And, if called with the empty list as its argument, this function should produce the value 0.)
Problem 4
Next, in your definitions window, type the comment lines:
;
; Problem 4
;
This function does NOT involve lists at all. It is here to "play" with random, and to provide a function we'll use on a later problem.
4 part a
We will try out Racket's random function in -- it expects one number argument, an integer, and returns a pseudo-random number (which does happen to be an integer) in [0, given integer).
One could use this to create random colors! make-color expects a red value, green value, and blue value, each in [0, 256):
(make-color (random 256) (random 256) (random 256))
Write two expressions of type image that use the above expression for their color argument -- you should (quite likely) see two different-colored images resulting in the Interactions window (and they'll (quite likely) be different each time you click run.)
4 part b
I decide that I would like a little function to simply produce a random-colored circle outline image of a specified radius.
Using the design recipe, design and write a function random-color-ring that expects a desired radius, and produces a circle outline image of that radius and of a random color.
Because this uses random, remember to write your tests/examples as described in the Important Notes section above.
Problem 5
Next, in your definitions window, type the comment lines:
;
; Problem 5
;
5 part a
We want to work with some lists of strings. So, develop a data definition comment for a StringList, (in the style of NumberList and ImageList), and then ALSO develop a comment containing a TEMPLATE for a function "walking through" a StringList.
5 part b
Following the design recipe, design and write a function emphasize-list that expects a list of strings, and it returns a new list of strings in which an ! has been added to the end of every string in the given list of strings. That is, if this function has as its argument a list of strings containing "Hey", "Oh my!", and "mooo", then it would return a new list of strings containing "Hey!", "Oh my!!", and "mooo!". (And, if called with the empty list as its argument, this function should simply return an empty list.)
Problem 6
Next, in your definitions window, type the comment lines:
;
; Problem 6
;
6 part a
Consider a list of numbers, in which each number represents a circle-ring radius.
Using the design recipe, develop a function many-circles, which expects a list of numbers representing circle-ring radii, and produces a scene containing circle-rings with those radii but of random colors, each placed in the center of the scene.
You are expected to appropriately use Problem 4 part b's random-color-ring function in your many-circles function.
Because this uses random, remember to write your non-empty-list tests/examples as described in the Important Notes section above.
6 part b
Define a named constant EX-RADII-LIST which is a list containing at least 6 different circle radii, each of which is considerably smaller than whatever WIDTH and HEIGHTconstants you are using for this homework's scenes.
Also copy over the function add1-list from the “even more Week 5 Samples” posted examples (be sure to include its signature, purpose, and tests, too).
Finally, write an expression calling add1-list with EX-RADII-LIST as an argument.
6 part c
Write a big-bang expression whose initial universe value is your EX-RADII-LIST, whose on-tick expression uses add1-list and whose to-draw expression uses your function many-circles.
What do you see when you run this? Describe what you see in a comment after your big-bang expression.