User Tools

Site Tools


cs330_f2016:rackethof2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs330_f2016:rackethof2 [2016/09/12 20:47]
morse
cs330_f2016:rackethof2 [2021/06/30 23:42] (current)
Line 16: Line 16:
   * ''​default-parms''​ - decorates a function to add default parameter values   * ''​default-parms''​ - decorates a function to add default parameter values
   * ''​type-parms''​ - decorates a function to enforce parameter type checking   * ''​type-parms''​ - decorates a function to enforce parameter type checking
 +  * ''​new-sin2''​ - a decorated version of your earlier ''​new-sin''​ that fills in default parameters and enforces correct types for all parameters
  
 You will also chain these together to add both parameter type checking and default parameters to a function. You will also chain these together to add both parameter type checking and default parameters to a function.
Line 43: Line 44:
 (define g (type-parms f (list number? string?)) (define g (type-parms f (list number? string?))
 </​code>​ </​code>​
-would result in a function ''​g''​ such that ''​(g 0 "​hello"​)''​ behaves just like ''​(f 0 "​hello"​)'',​ but passing anything but a number for the first parameter or anything other than a string for the second parameter results in an error message.+would result in a function ''​g''​ such that ''​(g 0 "​hello"​)''​ behaves just like ''​(f 0 "​hello"​)'',​ but passing anything but a number for the first parameter or anything other than a string for the second parameter results in an error message, by calling ``(error "​ERROR ​ MSG"​)``.
  
 Hint: You may want to write a small helper function that takes a list of values and a list of type predicates, then makes sure each of the values satisfies the respective type predicate. ​ This is straightforward to write recursively. ​ Can you write it in shorter form by mapping a two-parameter function pairwise on the lists? Hint: You may want to write a small helper function that takes a list of values and a list of type predicates, then makes sure each of the values satisfies the respective type predicate. ​ This is straightforward to write recursively. ​ Can you write it in shorter form by mapping a two-parameter function pairwise on the lists?
Line 55: Line 56:
 <code racket> <code racket>
 (define g (default-parms ​ (define g (default-parms ​
-              ​(type-parms f  +            ​(type-parms 
-                          (list number? string?))  +             f  
-              (list 0 ""​)))+             ​(list number? string?))  
 +            (list 0 ""​)))
 </​code>​ </​code>​
 would result in a function ''​g''​ that fills in any missing parameters with ''​0''​ and ''""''​ respectively,​ and then verifies that the passed parameters are a number and a string. would result in a function ''​g''​ that fills in any missing parameters with ''​0''​ and ''""''​ respectively,​ and then verifies that the passed parameters are a number and a string.
Line 64: Line 66:
   * uses ''​0''​ and '''​radians''​ as defaults for the parameters, and   * uses ''​0''​ and '''​radians''​ as defaults for the parameters, and
   * verifies that the first parameter is a number and that the second parameter is a symbol.   * verifies that the first parameter is a number and that the second parameter is a symbol.
 +This must be created using ''​default-parms''​ and ''​type-parms'',​ not hand-coded.
  
 ---- ----
Line 71: Line 74:
 You again **do not** need to bulletproof the code to enforce proper inputs.  ​ You again **do not** need to bulletproof the code to enforce proper inputs.  ​
 Your code only needs to return correct values given correct inputs. Your code only needs to return correct values given correct inputs.
 +
 +Note: catching incorrectly typed inputs is part of the specified functionality for the ''​type-parms''​ decorator, so the functions generated by it should specifically detect and report that type of error.
  
 ---- ----
- 
 ====Hints:​==== ====Hints:​====
  
-For both of your decorators, they have the same basic form:+Remember from class that our decorators have the same basic form:
 <code racket> <code racket>
 (define (<name of the decorator>​ f ...) (define (<name of the decorator>​ f ...)
Line 83: Line 87:
     (apply f args)))     (apply f args)))
 </​code>​ </​code>​
- 
cs330_f2016/rackethof2.1473713234.txt.gz · Last modified: 2021/06/30 23:40 (external edit)