CSC151 2009F, Class 37: Pairs and Pair Structures Admin: * Reading for Monday: Trees. * I anticipate updating the reading over the weekend (when I'm not grading or raking leaves or helping at the fun run or at Singers or ....). * You have a writeup on today's lab. * Quiz 8 (finally) returned. * Some are marked "Not counted" * Some are maked "Counts" * Some are marked "Do you want this to count" * Are there questions on Assignment 7? * We'll go over the palette-darken! procedure from the lab on vectors. * Are there pre-quiz questions on topics from the last week (randomness, vectors, strings, geometric art, ...)? * When you finish the quiz, start the lab. * One more week until Rent! Overview: * Questions on assignment 7 * Questions on quiz 8 * The palette-darken! procedure * Pre-quiz questions * Exploration of pairs * Pairs and cons cells. * Why care? * Quiz * Lab. About palette-darken! ; Problem: Write palette-darken! ; "Make it darker" ; "it is a vector of colors" ; Apply rgb-darker individually ; Changing each element in the vector as it goes ; Pretend we only have to change element with index 5 (define palette-darken! (lambda (palette) (vector-set! palette 5 (rgb-darker (vector-ref palette 5))))) ; Whoops! Need to do every position (define palette-darken! (lambda (palette) ; with each position, pos, from 0 to the length of the palette - 1 (for-each (lambda (pos) (vector-set! palette pos (rgb-darker (vector-ref palette pos)))) (iota (vector-length palette))))) ; But we don't get to use recursion, and we LOVE recursion. Really we do. (define palette-darken! (lambda (palette) ; Recursively generate the numbers from 0 to (vector-length palette)-1 (let kernel ((pos 0)) (cond ((= pos (vector-length palette)) palette) (else (vector-set! palette pos (rgb-darker (vector-ref palette pos))) (kernel (+ 1 pos))))))) (define palette-darken! (lambda (palette) ; Recursively generate the numbers from 0 to (vector-length palette)-1 (let kernel ((pos 0)) (when (< pos (vector-length palette)) (vector-set! palette pos (rgb-darker (vector-ref palette pos))) (kernel (+ 1 pos)))))) (define rainbow (define rainbow (vector RGB-RED RGB-ORANGE RGB-YELLOW RGB-GREEN RGB-BLUE RGB-INDIGO RGB-VIOLET)) Quiz answers: Problem 1. (define delete-char (lambda (str pos) (string-append (substring str 0 pos) (substring str (+ pos 1) (string-length str))))) Problem 2. vector-op! modifies the vector by adding 1 to each element of the vector. Sam's testing of delete-char > (map (lambda (pos) (delete-char "abcdefghijklmnopqrstuvwxyz" pos)) (iota (string-length "abcdefghijklmnopqrstuvwxyz"))) ("bcdefghijklmnopqrstuvwxyz" "acdefghijklmnopqrstuvwxyz" "abdefghijklmnopqrstuvwxyz" "abcefghijklmnopqrstuvwxyz" "abcdfghijklmnopqrstuvwxyz" "abcdeghijklmnopqrstuvwxyz" "abcdefhijklmnopqrstuvwxyz" "abcdefgijklmnopqrstuvwxyz" "abcdefghjklmnopqrstuvwxyz" "abcdefghiklmnopqrstuvwxyz" "abcdefghijlmnopqrstuvwxyz" "abcdefghijkmnopqrstuvwxyz" "abcdefghijklnopqrstuvwxyz" "abcdefghijklmopqrstuvwxyz" "abcdefghijklmnpqrstuvwxyz" "abcdefghijklmnoqrstuvwxyz" "abcdefghijklmnoprstuvwxyz" "abcdefghijklmnopqstuvwxyz" "abcdefghijklmnopqrtuvwxyz" "abcdefghijklmnopqrsuvwxyz" "abcdefghijklmnopqrstvwxyz" "abcdefghijklmnopqrstuwxyz" "abcdefghijklmnopqrstuvxyz" "abcdefghijklmnopqrstuvwyz" "abcdefghijklmnopqrstuvwxz" "abcdefghijklmnopqrstuvwxy")