|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
Due Oct.31 (Mon) 15:00.
Your name and the assignment-number must be in a comment at the start of the file, and your hardcopy must be stapled. All functions/data must include the appropriate steps of the-design-recipe—the design recipe: final version. In particular, test cases alone might be worth half the credit for a function. Unless otherwise indicated, two test cases will suffice for most problems, if they are testing different situations.
Complete problems in racket, except for 5 and 6 which you’ll complete in Java and
racket,
as instructed.
Your Java method should compile and run and be correct,
but then you can paste it into a racket block-comment (.
Unless otherwise indicated, all problems are to be written in Racket, in Beginning-student. Do not call any of the following functions:
hint: We can’t re-assign to variables, in functional programming. So we won’t have a counter which we increment, like you might in your imperative-programming class. Instead, be sure to follow the design recipe, and after copying the template, think about the inventory-with-values (assuming we call our parameters “threshold” and “nums”): if we callcount-bigs with the particular inputs(count-bigs 3 (cons 10 (cons 2 (cons 5 '())))) , what is…Fill in each blank with a particular number, or list-of-numbers. Then, ask yourself: Starting with those pieces of info, how do I assemble them to get my desired result of 2?
threshold =nums =(first nums) =(rest nums) =(count-bigs threshold (rest nums)) =(You don’t need to include the above in your answer — it’s just to remind you of what you do, for the “inventory-with-values” step of the design-recipe, #6. If you get stuck on any of the problems below, make sure you didn’t skip this step!)
(check-expect (map-sqr '()) '()) (check-expect (map-sqr (cons 7 '())) (cons 49 '())) (check-expect (map-sqr (cons 9 (cons 7 '()))) (cons 81 (cons 49 '()))) |
(5pts)
Write
hint: Follow the template for natural-numbers — not for lists!I will only run your code on lists that meet the pre-condition that the index will be less than the length of the list. (If it isn’t, it’s fine for your function to have a run-time exception.)
Write a function
Notice that
several of the image-creating functions in imported via
(5pts)
Let’s write a function
(check-expect (shapes-in-a-row (cons ellipse (cons right-triangle (cons rhombus '())))) (beside (ellipse 54 40 'solid 'orange) (beside (right-triangle 54 40 'solid 'orange) (beside (rhombus 54 40 'solid 'orange) empty-image)))) |
language:For this problem and the next, you’ll need to switch DrRacket's language level to “Intermediate Student with lambda” (so that you can mention a function without calling it). Although Intermediate Student also includes the functionlist , do not use it for this homework; build lists viacons . After this homework, use oflist will be allowed.
Extra credit (2pts): Without modifying your function, pass it a list containing
Extra Credit (2pts): using
Note: Note that being tail-recursive is a property of a function’s source-code. The fact that a tail-recursive function can be optimized to not unnecessarily-allocate stack space is a compiler-implementation issue — albeit it’s what makes the concept of tail-recursion important.
Reading:Scott also discusses recursion and tail-recursion, in §6.6.1.
/** Return the smallest number in a list. * @pre <code>!nums.isEmpty()</code> * @return the smallest number in `nums`. */ static Double min( List<Double> nums ) { // initialize our loop-variables: double minSoFar = nums.get( ); List<Double> numsRemaining = nums.subList( ); while ( ) { double a = numsRemaining.get(0); // corresponding to Scott’s variable `a` minSoFar = ( ? : ); numsRemaining = ; } return minSoFar; } |
Btw: The book’s starting-code calls(empty? (rest l)) — something not in the template. It’s a bit of a hack to stray from the template: Scott wants to avoid making a helper-function, but still return a sentinel-value answer for empty-lists.
However, when converting to tail-recursion, this difference ends up being moot: as you make a helper/wrapper function for the tail-recursion, that extra check disappears.
scheme vs racket: The book’s scheme code uses:car ,cdr ,null? , and#t . In racket, these names are (respectively):first ,rest ,empty? , and#true .
(check-expect (natnum->string/binary 0) "") (check-expect (natnum->string/binary 1) "1") (check-expect (natnum->string/binary 2) "10") (check-expect (natnum->string/binary 3) "11") (check-expect (natnum->string/binary 4) "100") (check-expect (natnum->string/binary 5) "101") (check-expect (natnum->string/binary 15) "1111") (check-expect (natnum->string/binary 16) "10000") ; natnum->string/binary : natnum -> string ; Return the binary-numeral representing n (without any leading zeroes). ; Note that the numeral for zero, without leading zeros, is the empty-string! ; (define (natnum->string/binary n) (cond [(zero? n) ""] [(positive? n) (string-append (natnum->string/binary (quotient n 2)) (if (even? n) "0" "1"))])) |
Btw: This code doesn’t quite follow the design-recipe for natural-numbers, because it usesquotient rather thansub1 . But it still works fine because it “reduces” the natnum to a smaller one. To reason about this code, you wouldn’t use straight-up mathematical induction; instead you'd call it “strong induction”.
1 Your final program doesn’t need to include any "transitional" results from the template: For example, you don’t need the stubbed-out version of a function from step 5. However, you should still have passed through this phase. ↩
2For example,
when sorting a list-of-songs, we might write
(Though in the case of
3In many languages, we'd call this the "adaptor pattern", and might write entire classes and interfaces, instead of just using an anonymous function. ↩
4
Realize that numbers, numerals, and digits are three distinct concepts.
In programming, the distinction becomes clear:
numbers/numerals/digits correspond to
5I'll accept answers which change either blue eyes, or brown eyes. ↩
7
Well, more seriously: we’ll do an equivalent tree-traversal in the future,
when working with parse-trees for a programming-language:
we’ll want to re-name all occurrences of a variable
6 People sometimes wonder what possible use-case such a function could have. Well: Sometimes you want to doctor an ancestor-tree, to make yourself appear to be irish. But if you have an ancestor who cried so much that their eyes were red — probably on hearing how you seem to be ashamed of your heritage — we won’t modify their ancestors out of respect. ↩
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
©2016, Ian Barland, Radford University Last modified 2016.Nov.19 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |