![]() |
![]() |
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
Due Nov.06 (Mon) in class and part(b) Nov.13 (Mon), hardcopy and on D2L.
Reading: Scott §3.6.4 (Lambda expressions); §11, but skip 11.4 and 11.5 (OCaml and Evaluation Order, respectively).
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 steps1 of 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.
For this (and future) assignments, bump up your DrRacket language-level to
Intermediate Student with
(4pts)
Write
data def: A natural-number is:
The predicates
- 0, OR
- (+ 1 [natural-number])
zero? andpositive? are often used to distinguish the two branches, though of course= and> could be used equally-well.
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 (both 3rd and 4th eds).
#|
/** Return the smallest number in a list.
* @pre <code>!nums.isEmpty()</code>
* @return the smallest number in `nums`.
*/
static Double myMin( List<Double> nums ) {
// initialize our loop-variables:
double minSoFar = nums.get( );
List<Double> numsRemaining = nums.subList( ,nums.size());
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 recurs on(quotient n 2) rather than(sub1 n) . 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”.
Recall:
the scope of identifiers introduced with
Recall: a variable-use's binding-occurrence is the place where that variable is defined.
Hint: The binding-occurrence itself is not considered a use of the variable. There are a total of seven uses ofa , amongst the four parts.
line 01 (define a 5) line 02 (define b 10) |
line A1 (let {[z 50] line A2 [a 51] line A3 } line A4 (+ a b z)) ; evaluates to: |
line B1 (let {[z 50] line B2 [a 51] line B3 [b (* a 3)] line B4 } line B5 (+ a b z)) ; evaluates to: |
line C1 (define (foo a) line C2 (let {[z 50] line C3 [a 51] line C4 [b (* a 3)] line C5 } line C6 (+ a b z))) line C7 (foo 1001) ; evaluates to: |
line D1 (let* {[z 50] line D2 [a 51] line D3 [b (* a 3)] line D4 } line D5 (+ a b z)) ; evaluates to: |
deferred:The followingtwofour problems will be due Nov.10 (Fri)13 (Mon) in class, as "hw0607b". We will go over passing functions andanc-tree s in lecture on Monday and Wednesday.
Notice that
several of the image-creating functions imported via
(3pts)
Let’s write a function
(check-expect (shapes-in-a-row |
(2pts) Pass your function a list containing
(2pts): using and as a one-liner.
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
| ©2017, Ian Barland, Radford University Last modified 2017.Dec.06 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland |