Description
Expectations

You and your partner must submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept no email submissions.

You must use the language specified at the top of this page.

Your code must conform to the guidelines outlined in the style guide on the course website. The style guide will be updated as the semester progresses, so revisit it before submitting each assignment. (You can resubmit as many times as you like until the due date without penalty.)

Unless otherwise stated, every function and data definition that you design, including helper functions, must follow all steps of the design recipe.
Finger Exercises HtDP/2e: 282, 283, 285, 288, 289
Matrices
Exercise 1 Design a function scalarmatrix, which, for given numbers n and k, produces an n×n matrix (a list of lists of numbers), where the diagonal entries (first of the first row, second of the second row, etc.) are k and all other entries are 0. For example, (scalarmatrix 3 2) should produce a representation of the following matrix

2 0 0
0 2 0
0 0 2
Exercise 2 Design a function diagonalmatrix, which, given a number n and a function f, produces a diagonal matrix, that is, an n×n matrix where all diagonal entries are generated by f as follows:

(f 0) 0 0 0
0 (f 1) 0 0
0 0 (f 2) 0
0 0 0 (f 3)
Write at least 3 checkexpects using lambda.
Exercise 3 Design a function scalarmatrix/v2 using diagonalmatrix.
Exercise 4 Design a function matrix>string, which converts a given matrix into a string representation, where each row occupies a separate line. For example (matrix>string (scalarmatrix 4 1)) should return a string containing

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
You must use list abstractions to solve this problem. Trailing spaces and newlines are fine. You can assume that each entry in the given matrix is a single digit.
Family trees
Exercise 5 Complete the data design recipe for the below data definition for a FamilyTree. Note that yob stands for year of birth.


(definestruct person [name yob parent1 parent2])
; A FamilyTree is one of:
; – “unknown”
; – (makeperson String Natural FamilyTree FamilyTree)

Exercise 6 Design the function maxage which computes the maximum age in a given FamilyTree. Assume that everybody was born on January 1st and is still alive. “unknown”’s age is 0.
Exercise 7 Design the function validft? which validates the given family tree. That is, it should return #true if the given FamilyTree is valid and #false otherwise. A FamilyTree is valid if and only if every person in the tree is younger than their parents.
Legos!
Let’s build some lego buildings out of lego bricks. Here are data definitions for lego bricks and lego buildings:

(definestruct lego (label color width))
; A Lego is a (makelego String String Number)
; Interpretation: (makelego l c w) is a lego brick
; with a label _l_, color _c_, and width _w_ (in pixels).
(definestruct bigger (lego left right))
; A LegoBldg (lego building) is one of:
; – Lego
; – (makebigger Lego LegoBldg LegoBldg)
; Interpretation: (makebigger l lft rgt) makes a bigger
; lego building by putting a lego brick _l_ on top of two lego
; buildings _lft_ (left) and _rgt_ (right).
Exercise 8 Design a function, countbricks, that takes a lego building and produces the total number of lego bricks in that building.
Exercise 9 Each lego brick is 10 pixels tall. Design a function, howhigh, that takes a lego building and produces the total height of the lego building (in pixels).
Exercise 10 Design a function, containscoloredbrick?, that takes a lego building and a color, and determines whether the building contains a lego brick of the given color.
Exercise 11 Design a function, findcoloredbrick, that takes a lego building and a color and finds any lego with the given color in the building, or returns #false if there are no such legos.
Here is the data definition for the type of data this function returns:


; A MaybeLego is one of:
; – #false
; – Lego

Your function should not use containscoloredbrick?, it should not traverse/examine parts of the building more than once, and it should stop searching once any brick of the given color is found.
Exercise 12 Design a function, lb>image, that takes a lego building and produces an image of the building.
Here are some examples:


> (lb>image (makebigger (makelego “4” “purple” 80)
(makebigger (makelego “2” “blue” 60)
(makelego “1” “yellow” 40)
(makelego “3” “red” 40))
(makebigger (makelego “6” “orange” 60)
(makelego “5” “green” 40)
(makelego “7” “red” 40))))
> (lb>image (makebigger (makelego “4” “purple” 80)
(makebigger (makelego “2” “blue” 60)
(makelego “1” “yellow” 40)
(makelego “3” “red” 40))
(makelego “6” “orange” 60)))
