Homework 6
Please read chapter 10: More on Lists and chapter 11: Design by Composition in the textbook. Also read section 5 (Local definitions) and 7 (Advanced types) in the Typed Racket Notes. We've skipped section 6 for now, but we'll come back to them. Local definitions appear in section 16.2 in the textbook. You may look ahead at that section now, if you like; however there are several examples there that involve things you have not learned yet, so you probably won't be able to understand all of it.
Be sure to include type, purpose, definition and tests for all functions.
Include the following at the top of your homework:
#lang typed/racket (require "../include/uchicago151.rkt") (require typed/test-engine/racket-tests)And include (test) at the bottom of your homework.
Problem 1
Write following functions on lists.- (: int-list-product (-> (Listof Integer) Integer)) to compute the product of a list of integers. For example, (int-list-product '()) is 1 and (int-list-product '(4 2 5)) is 40.
- (: evens (-> (Listof Integer) (Listof Integer))) to build a list only of the even numbers in the input. For example, if the input list contained 1, 2, 3, 4, 5, in that order, the list returned by the function would consist of 2 and 4.
- (: append-strings(-> (Listof String) String)) to build a string by appending together all the strings in a list. For example, if the input list contained "The", "quick", and " brown", in that order, the function output should be "Thequick brown".
- (: numbers (-> Nonnegative-Integer (Listof Positive-Integer))) to build a list of numbers from 1 to n. For example, (numbers 0) should be the empty list and (numbers 5) should be the list '(5 4 3 2 1). Order matters.
Problem 2
We define the following data type for lists of coins:
This creates a type, Coin that can have only five values. Those values are the strings "Dollar", "Quarter", "Dime", "Nickel", and "Penny".(define-type Coin (U "Dollar" "Quarter" "Dime" "Nickel" "Penny"))
- Write the function (: num-quarters (-> (Listof Coin) Integer)) that returns the number of quarters in the list.
- Write the function (: pct-dimes (-> (Listof Coin) Exact-Rational)) that returns the percentage of coins in the list which are dimes. I suggest writing one or two helper functions for this.
- Write the function (: contains-dollar? (-> (Listof Coin) Boolean)) that returns true if there are one or more dollar coins in the list.
- Write the function (: coin-total (-> (Listof Coin) Integer)) that returns the total value of the list of coins in pennies. For example, the total of a list of two quarters and a dime is 60.
Practice
If these functions are difficult, please spend time on the reading. The book has a lot of good exercises and examples that you should use for practice. Understanding lists in particular and recursive data structures in general is very important.Submit your work in your repository in hw6/hw6.rkt by noonMonday, July 3.