;CSC151.01 2006S, Class 38: Discussion of Exam 2 ; ;Admin: ;* Experiment: Doing everything in DrScheme ;* If you notice fewer students than normal, we had a surprising ; number of students withdraw. ;* Thursday extra this week (noon! pizza!): Functional programming and GIMP. ;* Questions about continuing in CS? Chat with me. ;* Prospies Friday! (Maybe). ;* Exam 2 returned. (At the end of class.) ; * Solutions to be distributed via email. ;* Questions on HW 12? ; * Reading for tomorrow: Association lists ; ;Overview: ;* General issues. ;* Problem 1. ;* Problem 2. ;* Problem 3. ;* Problem 4. ; General issues on the exam ; Overly verbose boolean expressions ; (if test? #t #f) ; (if (= (random 2) 1) #t #f) ; More concise to write (= (random 2) 1) ; Similar ; (if test1? test2? #f) ;(if (= (get-year s1) (get-year s2)) ; (if (<= 3 (count-identical-attributes s1 s2)) ; #t ; #f) ; #f) ;(if (= (get-year s1) (get-year s2)) ; (<= 3 (count-identical-attributes s1 s2)) ; #f) ;(and (= (get-year s1) (get-year s2)) ; (<= 3 (count-identical-attributes s1 s2))) ; Choose good procedure names and, perhaps, document them ;(define friend ; (lambda (...) ; ...)) ; Question mark has a meaning in variable and procedure names ; If a procedure ends with a question mark, it should return #t or #f (define good-matches (lambda (student list-of-students) (if (null? list-of-students) null (if (good-match? student (car list-of-students)) (cons student (good-matches (cdr list-of-students))) (good-matches (cdr list-of-students)))))) ; Don't name that "good-matches?" ; Don't name things you're only going to use once (unless the naming clarifies) (let* ((random-smokes (random 2)) (smokes (= random-smokes 0))) (vector smokes)) ; We can probably do without either name. ; Problem 1: index-of ; * Document ; * Implement ; * Test ; Sam is anal about format ;;; Procedure: ;;; index-of ; Hardest part: Postconditions ; Postconditions should be *formal* (not ambiguous) ; (vector-ref vec index) = val ; The "Produces" section should have named index ; What if the value appears multiple times? ; * Any copy ; * Multiple copies (define index-of (letrec ((kernel (lambda (val pos vec len) (if (= pos len) #f (if (equal? (vector-ref vec pos) val) pos (kernel val (+ pos 1) vec len)))))) (lambda (val vec) (kernel val 0 vec (vector-length vec))))) ; Unit tests: ; Goal of comprehensive ; Make Sam feel good about spending two days writing unit-test.ss ; Easier to reproduce ; Automate ; Makes it easier to see the points at which errors occur ; YOU DON'T HAVE TO THINK ABOUT THE OUTPUT TO MAKE SURE THAT IT WORKED ; Comprehensive: ; * Check different types ; * Check different positions, particularly special cases ; * Check "normal" cases: Not in Vector, in the middle ; * Empty vectors ; Problem 2 ; Deep recursion (define symbols->string (letrec ((kernel (lambda (symbols) (cond ((null? symbols) "") ((symbol? symbols) (string-append " . " (symbol->string symbols))) (else (string-append " " (symbols->string (car symbols)) (kernel (cdr symbols)))))))) (lambda (symbols) (cond ((null? symbols) "()") ((symbol? symbols) (symbol->string symbols)) (else (string-append "(" (symbols->string (car symbols)) (kernel (cdr symbols)) ")"))))))