Homework is no longer required. Do it for more practice on the topics covered in lecture. Email it to yuhu@cs.uchicago.edu and put your name and the homework number in the Subject line of your message if you want to get feedback on it.All assignment numbers come from the online version of the text.They may be slightly different in the first and second printing (but they match the third printing). 
Due Date  Language (in DrScheme)  Problems 

11/17  Intermediate Student w/lambda  Write the function:
;; samegraph? : (graph X) (graph X) > boolean ;; to determine g1 and g2 have the same nodes, ;; and each node in g1 has the same neighbors as that node in g2. ;; assume that both graphs have the same node equality function. (define (samegraph? g1 g2) ...)
;; examples as tests (samegraph? (makegraph '() (lambda (x) '()) symbol=?) (makegraph '() (lambda (x) '()) symbol=?)) true
(samegraph? (makegraph '(a) (lambda (x) '()) symbol=?) (makegraph '() (lambda (x) '()) symbol=?)) false
(samegraph? (makegraph '(a) (lambda (x) '()) symbol=?) (makegraph '(a) (lambda (x) '()) symbol=?)) true
(samegraph? (makegraph '(b) (lambda (x) '()) symbol=?) (makegraph '(a) (lambda (x) '()) symbol=?)) false
(samegraph? (makegraph '(a b) (lambda (x) '()) symbol=?) (makegraph '(b a) (lambda (x) '()) symbol=?)) true
(samegraph? (makegraph '(a b) (lambda (x) (cond [(symbol=? x 'a) '(b)] [(symbol=? x 'b) '()])) symbol=?) (makegraph '(a b) (lambda (x) (cond [(symbol=? x 'b) '(a)] [(symbol=? x 'a) '()])) symbol=?)) false
(samegraph? (makegraph '(a b) (lambda (x) (cond [(symbol=? x 'a) '(a b)] [(symbol=? x 'b) '()])) symbol=?) (makegraph '(a b) (lambda (x) (cond [(symbol=? x 'a) '(b a)] [(symbol=? x 'b) '()])) symbol=?)) true
Write the following two functions and use samegraph? to test them.
;; reverseedgegraph : (graph X) > (graph X) ;; to build a graph with the same nodes as g, but with reversed edges. ;; that is, if g has an edge from a to b then the result graph will have an edge from b to a. (define (reverseedgegraph g) ...)
;; undirected? : (graph X) > boolean ;; to determine if each edge in g has a matching edge going the opposite direction (define (undirected? g) ...)
Write the following functions using an accumulator.
;; posnsum : (listof posn) > posn ;; compute a posn whose x coordinate is the sum of the x coordinates in ps ;; and whose y coordinates is the sum of the y coordinates in ps (define (posnsum ps) ...)
;; digits>num : (listof digit) > number ;; to compute the number represented by the list of digits (define (digits>num ds) ...)
;; examples as tests: (digits>num (list 1 2 3)) 123 (digits>num empty) 0
Using this data definition for a family tree, define the function below using an accumulator. ;; a familytree is either: ;;  empty ;;  (makenode symbol familytree familytree) (definestruct node (name mom dad))
;; second? : familytree > boolean ;; to determine if any child has the same name as an ancestor of theirs. (define (second? ft) ...)

11/15  Intermediate Student w/lambda  Revisit the function reverse from earlier in class. It takes a list of numbers and returns the same list, but in the reverse order. Write it out (following the design recipe as you did before.)
This function suffers from the same flaw as the foodchain function in lecture. Rewrite it using an accumulator to fix the problem. Reuse your tests from above to make sure it still is right.
Write the product function using an accumulator (it takes a list of numbers and returns their product). 
11/13  Intermediate Student w/lambda  Implement findpaths.
; a (graph X) is: ; (makegraph (listof X) (X > (listof X) (X X > boolean)) (definestruct graph (nodes neighbors nodeequal?))
; findpaths : (graph X) X X > (listof (listof X)) ; to find all of the paths in the graph from src to dest (define (findpaths graph src dest) ...)

11/10  Intermediate Student w/lambda  HtDP 26.3.1, 26.3.2, 26.3.3, 26.3.4 
11/8  Intermediate Student w/lambda  HtDP: 25.2.2, 25.2.3, 25.2.4, 25.2.6 For 25.2.3, use (time ...) to determine what the cutoff should be before switching from quicksort to sort. For example, (time (quicksort (forgelist 100 (lambda (x) (random 1000))))) tells you how long quicksort takes to sort a list of 100 random numbers. 
11/6  Intermediate Student w/lambda  Write the following functions. You may not use +, * or expt. ;; add : nat nat > nat ;; to add the two nats together
;; mul : nat nat > nat ;; to multiply the two nats together
;; pow : nat nat > nat ;; to compute x to the nth power (define (pow x n) ...)
;; forgelist : nat (nat > X) > (listof X) ;; to forge a list of length N where the ;; last element is (f 0), the second to last is (f 1), etc (define (forgelist n f) ...)
;; examples as tests (forgelist 4 (lambda (x) x)) (list 3 2 1 0)
;; triangularlist : nat > (listof (listof 'x)) ;; to build a triangular list of lists of the symbol 'x (define (triangularlist i) ...)
;; examples as tests (triangularlist 4) (list (list 'x) (list 'x 'x) (list 'x 'x 'x) (list 'x 'x 'x 'x)) 
11/3  Intermediate Student w/lambda  a) Hand evaluate: (show all steps)
(define mul (lambda (x) (lambda (y) (* x y)))) (define m3 (mul 3)) (+ (m3 3) (m3 4))
b) Hand evaluate: (show only recursive calls)
(map (lambda (x) (expt x 3)) (list 1 2 3))
b) Hand evaluate: (show only recursive calls)
(filter (lambda (x) (zero? (modulo x 3))) (list 5 6 7))
If you don't know what some of the primitives above do, try them out in the interactions window. 
11/1  Intermediate Student  Do not write any recursive functions for this homework. map, filter, and foldr are built in to DrScheme.
Use map to write the next function.;; buildstraightline : num (listof num) > (listof posn)
;; returns a list of posns where the X coordinate is n and
;; the Y coordinate is a number
;; in lon
;; e.g., (buildstraightline 2 (list 1 2 3))
;; (list (makeposn 2 1) (makeposn 2 2) (makeposn 2 3))
(define (buildstraightline n lon) ...)
Use filter to write the next function.;; ptsnorth : posn (listof posn) > (listof posn)
;; returns the posns from lop that are north of p,
;; that is, whose y coordinate is greater than p's y coordinate
(define (ptsnorth p lop) ...)
Use foldr to write the next function.;; totalwidth : (listof image) > num
;; returns the sum of the widths of all images in loi
(define (totalwidth loi) ...)
Use map filter and foldr to write the next four functions.
The next exercises use functions to represent curves in the plane. A curve can be represented as a function that accepts an x coordinate and returns a y coordinate. For example, the straight line through the origin can be represented as (define (diagonal x) x) and a parabola sitting on the origin can be represented as (define (parabola x) (* x x))
(define points (list (makeposn 1 0) (makeposn 1 1) (makeposn 2 2)))
;; pointsonline : (num > num) (listof posn) > (listof posn)
;; return the points in pts that are on the curve described by f
;; e.g.
;; (pointsonline diagonal points) 'shouldbe (list (makeposn 1 1) (makeposn 2 2))
;; (pointsonline parabola points) 'shouldbe (list (makeposn 1 1))
(define (pointsonline f pts) ...)
;; positions: (num > num) (listof num) > (listof posn)
;; returns a list of positions on the curve `f' whose xcoordinates
;; are in lon
;; e.g., (positions parabola (list 1 2 3))
;; (list (makeposn 1 1) (makeposn 2 4) (makeposn 3 9))
(define (positions f lon) ...)
;; flattenposns : (listof posn) > (listof num)
;; constructs the list consisting of all the X and Y coordinates of each
;; of the posns in lop, in order.
;; e.g., (flattenposns points) 'shouldbe (list 1 0 1 1 2 2)
(define (flattenposns lop) ...)
;; possibleycoords : (listof (num > num)) num > (listof num)
;; given a list of lines lof and an xcoordinate, returns the list
;; of what ycoordinate is associated with that xcoordinate in each curve
;; e.g. (possibleycoords (list diagonal parabola) 7)
;; (list 7 49)
(define (possibleycoords lof x) ...)

10/30  Intermediate Student  Write bestimage:
; bestimage : (image image > boolean) nonemptylistofimages > image ; to find the best image in a nonempty list of images, ; where pred indicates if one image is better than another (define (bestimage pred aneloi) ...)
Use bestimage to redefine the two functions that were due on 10/23. 
10/27  Intermediate Student  HtDP: 15.1.2, 15.1.3, and 15.1.4 
10/23  Intermediate Student  For this assignment, you must not duplicate any recursive calls, and if you write any helper functions, enclose them in a local.
Consider the data definition and template for nonempty list of images: ; a nonemptylistofimages is either ;  (cons image empty) ;  (cons image nonemptylistofimages)
;; neloitemplate : nonemptylistofimges > ??? (define (neloitemplate aneloi) (cond [(empty? (rest aneloi)) ... (first aneloi) ...] [else ... (first aneloi) ... ... (neloitemplate (rest aneloi)) ...]))
; narrowestimage : nonemptylistofimages > image ; to find the image with the smallest width
; darkestimage : nonemptylistofimages > image ; to find the image with the lowest average color component ; (considering all three color components in each color)

10/20  Beginning Student w/List Abbreviations  Write the functions: ; sumpairs : listofnumbers listofnumbers > listofnumbers ; produces a list of the pairwise sums of the numbers in alon1 and alon2 (define (sumpairs alon1 alon2) ...)
; examples as tests (sumpairs empty empty) = empty (sumpairs (list 1 3 5) (list 9 17 2001)) = (list 10 20 2006)
; allin : listofnumbers listofnumbers > boolean ; determines if all of the numbers in alon1 are in alon2 (define (allin alon1 alon2) ...)
; examples as tests (allin empty empty) = true (allin (list 1) empty) = false (allin empty (list 1)) = true (allin (list 1) (list 3 1 4)) = true (allin (list 3 1 4) (list 3)) = false

10/18  Beginning Student w/List Abbreviations  HtDP: 14.2.3, 14.2.5, 14.2.6 
10/16  Beginning Student w/List Abbreviations  Write the following functions.
;; above : image image > image ;; to put one image above another ;; HINT: use putpinhole to move the pinhole of ;; the first to the middle of the bottom edge ;; and the second to the middle of the top edge (define (above i1 i2) ...)
;; a lego is ;;  (makelego symbol number) (definestruct lego (color width))
;; lego>image : lego > image ;; to render a lego brick. All legos are rectangular ;; and are 10 pixels tall (define (lego>image l) ...)
;; a legobuilding is either: ;;  lego ;;  (makebigger lego legobuilding legobuilding) (definestruct bigger (lego left right))
;; howhigh : legobuilding > number ;; to determine how high a lego building is, in pixels ;; (reminder: each lego is ten pixels tall) (define (howhigh anlb) ...)
;; findcoloredbrick : legobuilding color > lego or false ;; to find any colored brick with the color `color' in anlb ;; or return false if there are no such legos. (define (findcoloredbrick anlb color) ...)
;; lb>image : legobuilding > image ;; to render a lego building into an image (define (lb>image anlb) ...)
;; Examples as tests ;; (make more tests yourself  these should be your last tests!)
(lb>image (makebigger (makelego 'blue 100) (makebigger (makelego 'green 60) (makelego 'orange 40) (makelego 'purple 40)) (makebigger (makelego 'pink 60) (makelego 'orange 40) (makelego 'purple 40))))
(lb>image (makebigger (makelego 'lightblue 80) (makebigger (makelego 'blue 40) (makebigger (makelego 'green 20) (makelego 'orange 10) (makelego 'purple 10)) (makebigger (makelego 'pink 20) (makelego 'orange 10) (makelego 'purple 10))) (makebigger (makelego 'salmon 40) (makebigger (makelego 'green 20) (makelego 'orange 10) (makelego 'purple 10)) (makebigger (makelego 'pink 20) (makelego 'orange 10) (makelego 'purple 10)))))

10/13  Beginning Student w/List Abbreviations  Write the following functions. ; a ftn is either ;  'unknown, or ;  (makechild symbol number symbol ftn ftn) (definestruct child (name date eyes mom dad))
; 40yearoldancestor? : ftn > boolean ; to determine if a 40yearold ancestor is in aftn (define (40yearoldancestor? aftn) ...)
; count : ftn > number ; to count the number of people in aftn (define (count aftn) ...)
; avgage : ftn > number ; to determine the average age in anftn (define (avgage aftn) ...) Hint: use helper functions

10/11  Beginning Student w/List Abbreviations  Use the webitunes teachpack for this exercise.
;; renderalbums : listofalbum > html ;; build a web page like we did in class, but include ;; all of the songs with each album, in a table inside a table. (define (renderalbums anloa) ...)
Use this expression (savewebpage "index.html" "Robby's Music" robbysitunes) to test your functions on some large inputs. Hint: only do this when you have tested all of your functions carefully with small inputs! 
10/9  Beginning Student  ; sqnums : listofnumbers > listofnumbers ; to square each element in alon (define (sqnums alon) ...)
; rev : listofnumbers > listofnumbers ; to reverse the elements in alon (define (rev alon) ...) Hint: break down complex tasks into smaller ones. Wishlists!
Images: 4.1  4.3 
10/6  Beginning Student  Write the following functions. Don't forget about reuse and helper functions (the functions we wrote in class are fair game for reuse as helper functions). ; a listofsymbols is either ;  empty, or ;  (cons symbol listofsymbols)
; containsdoll? : listofsymbols > boolean ; to determine if 'doll appears in alos (define (containsdoll? alos) ...)
; a listofnumbers is either ;  empty, or ;  (cons number listofnumbers)
; len : listofnumbers > number ; to determine the number of elements in alon (define (len alon) ...)
; avg : listofnumbers > number ; to determine the average of the elements in alon (define (avg alon) ...) 
10/4  Beginning Student  HtDP: 7.2.2 (make sure all vehicles have wheels)
Develop the function toll : vehicle > number. It determines the amount a vehicle must pay at a toll. The toll costs $0.50 per wheel.
Extend the animal data definition from class with one new kind of animal. Make sure the new animal has a weight.
Write a template for the extended animal data definition.
Write the function diet : animal > animal. It accepts an animal and returns the same animal, but with half of the weight. 
10/2  Beginning Student  Write the function directto0 : posn > number that determines how far a posn is from the origin, as the crow flies.
Write the function downtownto0 : posn > number that determines how far a posn is from the origin, if the coordinates were block numbers in downtown Chicago. That is, you cannot cut through buildings and have to walk on streets (this is commonly called "Manhattan" distance, but we know better).
Write the function directbetween : posn posn > number that determines the distance between two points, as the crow flies. (Hint: wishlists & reuse!)
Images: 3.1  3.2 
9/29  Beginning Student  Images: 2.1  2.3 
9/27  Beginning Student  Images: 1.1  1.5 