CS 15100 Fall 2004

LecturesMWF 9:30 - 10:20 Ryerson 276
lecture9.pdf
lecture20.pdf

LabW 3:30 - 4:50 JRL A-level (Mac lab)
Lab webpage

ExamsExam 1: 10/18 [Solution: html pdf]
Exam 2: 11/15 [Solution: html pdf]
Final Exam: Fri. Dec. 10, 8:00-10:00am, Ry 276 [Solution: html pdf]

TextHow to Design Programs

Supplementary
Reading
The Little Schemer
by Freidman and Felleisen

Syllabus
WeekTopicReadings
9/27, 9/29, 10/1Basic forms of DataCh 2 - 6
10/4, 10/6, 10/8Unions and ListsCh 7 - 10
10/11, 10/13, 10/14Lists, Trees & Iterative RefinementCh 12 - 16
10/18 (exam 1), 10/20, 10/222 Complex Pieces of Data, LocalCh 17 - 18
10/25, 10/27, 10/29AbstractionCh 19 - 23
11/1, 11/3, 11/5Abstraction, review/stretchCh 19 - 23
11/8, 11/10, 11/12Generative RecursionCh 25 - 26
11/15 (exam 2), 11/17, 11/19Evaluation, AccumulatorsCh 14, 17, 30 - 33
11/22, 11/24Accumulators & Generative RecursionCh 30 - 33
11/29, 12/1Accumulators & Abstraction, Representation IndependenceCh 30 - 33

Mailing ListSubscribe Archive

SoftwareDrScheme

Lab
Space
CS Lab

Grading
Homeworks:10%
Exam One:25%
Exam Two:25%
Final Exam:40%

Course
Staff
Robby Findler
Office Hours: by appt
Office: Hinds B-031

Jacob Matthews
Office: Hinds B-026

Chunyan Song

HomeworkHomework is due at the beginning of class.
Print it out and hand it in.
Due DateLanguageProblems
11/31Intermediate Student w/lambdaDo exercises 31.3.3, 31.3.4, and 31.3.7 from HtDP using foldl (the accumulator version of fold).

;; foldl : (listof X) Y (X Y -> Y) -> Y
(define (foldl l a combine)
  (cond
    [(empty? l) a]
    [else (foldl (rest l) (combine (first l) a) combine)]))
11/24Intermediate Student w/lambdaHtDP: 31.3.4, 31.3.6, 31.3.7
11/22Intermediate Student w/lambdaWrite the function reverse from earlier in class. It takes a list of numbers and returns the same list, but in the reverse order.

This function suffers from the same flaw as the food-chain function in lecture. Rewrite it using an accumulator to fix the problem.

Write the product function using an accumulator (it takes a list of numbers and returns their product).
11/19Intermediate Student w/lambdaHtDP: 14.4.1, 14.4.2, 14.4.3
11/12Intermediate Student w/lambdaHtDP: 25.2.2, 25.2.3, 25.2.4
11/10Intermediate Student w/lambdaA small boy decides to climb the 10ft tall tree in his front yard. The tree has one branch each foot. As the boy climbs the tree, one of these three things happens (at each branch):
  • he falls off the tree, with a 1:100 chance;
  • he gets scared and decides to climb down one branch;
  • he continues climbing;
The boy gets scared with a chance 1 in z*2 if he is z feet from the top of the tree. If the boy gets scared when he's on the ground, he gives up.

Write a function that models the boy's behavior as he climbs the tree
;; climb-tree : number -> 'made-it 'scared-down or 'fell-off
(define (climb-tree height) ...)

Test your function by putting several calls to it (with 0 as an argument) in the bottom of the definitions window, since it won't always return the same result.

Here is a useful helper function:
;; 1-in-X-chance : Nat -> boolean
;; returns true randomly, with odds 1:X
;; That is, 1-in-X-chance always returns true if x is 1
;; 1-in-X-chance returns true half of the time if X is 2, etc.
(define (1-in-X-chance x) (zero? (random x)))
11/8Intermediate Student w/lambdaHtml exercises 1.1 - 1.3
11/3Intermediate Student w/lambdaa) 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/1Intermediate StudentDefine the following functions in terms of fold, filter, or map:

; double-each : list-of-numbers -> list-of-numbers
; to double each of the elements in alon
(define (double-each alon) ...)

; keep-perfect-sq : list-of-numbers -> list-of-numbers
; to remove each non-perfect square from alon
(define (keep-perfect-sq alon) ...)

; sum-of-squares : list-of-numbers -> number
; to sum up the squares of all the numbers in alon
(define (sum-of-squares alon) ...)

; howmany : list-of-list-of-numbers -> number
; to count the number of numbers in alolon
(define (howmany alolon) ...)

; example as test
(howmany (list (list 1 2 3) (list 4 5 6) (list 7 8 9)))
'shouldbe 9

Recall family trees:
; a ft is either:
; - 'unknown
; - (make-node ft ft symbol number)
(define-struct node (mom dad eyes year))

Define the fold function for family trees (based on the template) and re-define blue-eyed-ancestor? : ft -> boolean in terms of the new fold function.

If you define any recursive helper functions, be sure to use fold, filter or map for them (if possible).
10/27Intermediate StudentConsider the data definition and template for non-empty list of images:
; a non-empty-list-of-images is either
; - (cons image empty)
; - (cons image non-empty-list-of-images

;; neloi-template : non-empty-list-of-imges -> ???
(define (neloi-template a-neloi)
  (cond
    [(empty? (rest a-neloi)) ... (first a-neloi) ...]
    [else ... (first a-neloi) ...
          ... (neloi-template (rest a-neloi)) ...]))
(See the exam sample solution for an example function using this data definition and template.)

Write best-image:

; best-image : (image image -> boolean) non-empty-list-of-images -> image
; to find the best image, according to the predicate, pred
(define (best-image pred a-neloi) ...)

Using best-image write the following functions:

; narrowest-image : non-empty-list-of-images -> image
; to find the image with the smallest width

; biggest-image : non-empty-list-of-images -> image
; to find the image with the largest area

; darkest-image : non-empty-list-of-images -> image
; to find the image with the lowest average color component
; (considering all three color components in each color)

Don't forget: helper functions!
10/25Intermediate StudentWrite the function:
; reverse : list-of-numbers -> list-of-numbers
; reverses the elements in a-lon
(define (reverse a-lon) ...)

using the helper function:

; add-at-end : number list-of-numbers -> list-of-numbers
; to produce a list of numbers with the elements of a-lon followed by n
(define (add-at-end n a-lon) ...)

Combine those two functions into a single function, using local so that add-at-end can only be called by reverse.

Write the function:
; max : list-of-numbers -> max
; to determine the biggest element in a-lon,
; assuming that all elements are positive
(define (add-at-end n a-lon) ...)

without using a helper function, but use local to avoid repeated computation in the body.
10/22Beginning Student w/List AbbreviationsFinish the lab. (Only do refinement 1, skip the rest.)
10/15Beginning Student w/List AbbreviationsWrite the function (and assocated helper functions):
; find : dir string -> boolean
; to determine if a file with the content `content' is in `dir'
; (the content must match exactly)
(define (find a-dir content) ...)
Useful helper function: string=? : string string -> boolean. It determines if two strings match exactly. Don't forget: strings are written like this: "abcdef".

Adapt the data definition from class to support files that have permissions. A file's permissions should be one of 'read-write 'read-only 'write-only.

Adapt your find function to only search in files that can be read.

Here is the final data definition from class.
; a file is:
; - (make-file symbol string)
;
; a dir-content is either:
; - empty
; - (cons file dir-content)
; - (cons dir dir-content)
;
; a dir is:
; - (make-dir name dir-content)
(define-struct file (name content))
(define-struct dir (name content))
10/13Beginning Student w/List AbbreviationsWrite the following functions.
; a ftn is either
; - 'unknown, or
; - (make-child symbol number symbol ftn ftn)
(define-struct child (name date eyes mom dad))

; 40-year-old-ancestor? : ftn -> boolean
; to determine if a 40-year-old ancestor is in a-ftn
(define (40-year-old-ancestor? a-ftn) ...)

; count : ftn -> number
; to count the number of people in a-ftn
(define (count a-ftn) ...)

10/11Beginning Student w/List AbbreviationsImages: 4.1 - 4.3
10/8Beginning StudentWrite the following functions. Don't forget about re-use and helper functions (the functions we wrote in class are fair game for re-use as helper functions).
; a list-of-symbols is either
; - empty, or
; - (cons symbol list-of-symbols)

; contains-doll? : list-of-symbols -> boolean
; to determine if 'doll appears in alos
(define (contains-doll? alos) ...)

; a list-of-numbers is either
; - empty, or
; - (cons number list-of-numbers)

; len : list-of-numbers -> number
; to determine the number of elements in alon
(define (len alon) ...)

; avg : list-of-numbers -> number
; to determine the average of the elements in alon
(define (avg alon) ...)
10/6Beginning StudentHtDP: 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/4Beginning StudentWrite the function dist-to-0 : posn -> number that determines how far a posn is from the origin.
HtDP: 6.3.1 (1, no box drawing), 6.4.1(1), 6.5.1(1)
10/1Beginning StudentImages: 2.1 - 2.3
9/29Beginning StudentHtDP: 2.2.5
Images: 1.1 - 1.5