User Tools

Site Tools


cs330_f2016:rackethof2

This is an old revision of the document!


THIS LAB IS NOT FINALIZED, DO NOT USE

Objective:

Learn more about higher-order functions:

  • Simple type checker (composing type guards?)
  • Introducing named parameters, or parameter defaults
  • Chaining decorators (and make sure the default parameters are of the correct type!)
  • Dump arguments
  • Decoration
  • Memoization
  • Counting calls
  • Profiling (?)
  • Finite-difference gradient approximation
  • Y-combinator (?)
  • Trace functions - print args, indent proportional to depth, add function tags

Preparation:

You will again use DrRacket, which you should have downloaded and installed for the previous labs.


Deliverables:

For this lab, you will need to implement the following decorator functions in Racket:

  • check-parms - decorates a function to enforce parameter type checking
  • default-parms - decorates a function to add default parameter values

You will also chain these together to add both parameter type checking and default parameters to a function.

default-parms

(define (default-parms f values) ...)

where f is a function of arbitrary arity, values is a list of default parameter values of the same length as the arity of f, and the result is a function that behaves exactly like f but fills in corresponding default parameter values from the values list if given fewer parameters than expected.

For example, if f takes two parameters,

(define g (default-parms f (list 42 99))

would result in a function g such that (g) behaves the same as (f 42 99), (g 8) behaves the same as (f 8 99), and (g 8 9) behaves the same as (f 8 9).

Hint: You might find it useful to use the Racket function list-tail, which returns the rest of a list after some number of elements. For example, (list-tail (list 1 2 3 4 5) 2) returns the list (3 4 5).

type-parms

(define (type-parms f types) ...)

where f is a function of arbitrary arity, types is a list of type predicates (e.g., number?, string?, etc.) of the same length as the arity of f, and the result is a function that behaves exactly like f but first checks each parameter passed in to make sure it is the correct type.

For example, if f takes two parameters,

(define g (type-parms f (list number? string?))

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.


Chaining Decorators

If default-parms and type-parms are working correctly, you should be able to chain them together like this:

(define g (default-parms (type-parms f (list number? string?)) 0 ""))

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.


Notes:

You again do not need to bulletproof the code to enforce proper inputs. Your code only needs to return correct values given correct inputs.


Hints:

cs330_f2016/rackethof2.1473480300.txt.gz · Last modified: 2021/06/30 23:40 (external edit)