Homework 6
Please read chapter 12: Projects: Lists, chapter 13: Summary, the Intermezzo: Quote, Unquote, and 14: Similarities Everywhere in the textbook. By now you should be familiar with section 1,2,3,4,5 and 7 (not 6, yet) in the Typed Racket Notes. Please review them, if you need to. You should also begin reading section 8: Higher-order programming. You should read the sections labeled "Functions are first-class values", "Function types", "Functions as arguments", "Functions as results", and "Functions embedded in data structures". We will finish section 8 soon, but we aren't quite there yet.
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
Use the following definition for russian dolls:(define-type RD (U Image-Color Layer)) (define-struct Layer ([color : Image-Color] [doll : RD]))
Write a function create-dolls : (Listof Image-Color) -> RD that takes a list of colors and turns them into a RD. For example: (create-dolls (list 'red 'blue 'yellow)) should create the RD (Layer 'red (Layer 'blue 'yellow))
Write a function rd->image : RD -> Image that creates an image of the RD. If the RD has no layers, then it is a single solid color square of side length 10. Each layer should be another square that is 10 pixels wider than the previous one.
Problem 2
Add the following to your definitions window:
; Gives as output a string with only the first character of the input ; Gives an error for empty string (: string-first (-> String String)) (define (string-first s) (substring s 0 1)) ; Gives as output all the characters of the input except the first ; Gives an error for empty string (: string-rest (-> String String)) (define (string-rest s) (substring s 1))
Use these functions to write the following recursive functions on strings: str-num-vowels, string-index, strlen, string-take and string-drop.
The str-num-vowels function should take a String as input and output a Nonnegative-Integer indicating the number of lower case vowels (a, e, i, o, u) in the input string. For example, (str-num-vowels "Hello") is 2 and (str-num-vowels "A longer string") is 3.
The string-index function should take a String s and an Integer n as input and produce a new string that contains only the single letter in position n. For example, (string-index "Hello" 0) should produce the string "H" and (string-index "This is a string" 10) should produce "s".
The strlen function should do the same thing as the built in string-length function: it should take a String as input and compute the number of letters in that string.
The string-take function should take a string s and an integer n as input and produce a new string that contains the first n characters of s. The function string-drop should take the same two inputs and do the opposite: produce as output a string that contains all the characters except the first n of them. For example, (string-take "Hello" 2) should output "He" and (string-drop "Hello" 2) should output "llo".
The only string functions you are allowed to use are:
string-first string-rest string=? string-append
Practice
If you are still having difficulty with lists, I recommend trying some of the projects in chapter 12. Since I haven't assigned them, you can work on them in groups and share your solutions (or partial solutions) on Piazza.
Another great exercise is to modify homework 5 problem 1. You should make a new World structure that has a list of balls instead of three balls.
Submit your work in your repository in hw7/hw7.rkt by 6pm Thursday, July 6.