Com S 227 meeting -*- Outline -*- context: see more of how procedures and recursion work. have them do eq?-los-list problem before this. * Tracing and Debugging (2.5) we've traced the execution of some procs we'll see how Scheme can help with the details ** an answer table like equational tracing, but gives names to recursive calls model of what scheme does do this for eq?-los-list of the quiz or swapper fill in part of the following, answer-2, have them continue with the rest in groups. -------------- ANSWER TABLE (DEFINE eq?-los-list ; TYPE: (-> (symbol (list symbol)) ; (list boolean)) (LAMBDA (s los) ; ENSURES: result is a list of the ; length as los, and the ith element ; of result is #t iff ith element of ; los is s (COND [(null? los) '()] [else (cons (eq? s (car los)) (eq?-los-list s (cdr los)))]))) (eq?-los-list 'm '(h m b)) answer-1 is: (cons #f answer-2) answer-2 is: answer-3 is: answer-4 is: So answer-1 is: ------------- write it out on the board so they can see it happening. ** how can the computer help tracing? like to have the computer track details of such procedures. *** remove-1st-trace run it on an example (remove-1st-trace 'e '(a b c d)) show this from the ch2.ss file *** entering, leaving show them ** tools for writing entering and leaving first some tools *** writeln this is needed for printing trace info show examples that it takes many args (like list) and prints them without blanks between and then a newline (hence the ln) ----------- WRITELN writeln : (-> (datum ...) void) (writeln 'shake 'a 'can) (writeln "a string") (writeln "3 + 4 = " (+ 3 4)) ----------- Note the code for writeln is in /home/cs227/springer-friedman.ss this is automatically loaded. It's automatic also in PC Scheme (may not be in others.) draw picture, with side effect output coming out the side. "a string" | [ writeln ] =prints=> a string contrast this with the picture for (+ 3 4) 3 4 | | [ + ] | v 7 distinguish printing from returning a value *** strings helpful for spacing in output surrounded by double quotes (as in load) outer quotes not displayed by writeln ----------- STRINGS "a string" "a string with \"quotes\" in it" (writeln "a string " 'and " a string with \"quotes\" in it") ----------- to get a \ in the string type \\ (e.g., PC file names) *** sequencing with begin give some examples of begin with writeln (begin 1 2) ==> 2 draw pictures of this |--------| | [1] | | | | | v | | poof | | | | [2] | |---|----- v 2 (define x 4) (begin (writeln "x is " x) (+ x 3)) prints: x is 4 ==> 7 |----------| | [writeln]=prints=> x is 4 | | | x 3 | | \ / | | [+] | | | | |-----|----| v 7 distingusih printing from returning (talk about read-eval print) ----------- THE BEGIN SPECIAL FROM (define x 4) (begin (writeln "x is " x) (+ x 3)) prints: x is 4 ==> 7 SYNTAX (begin expr1 ... exprn) ------------ want to print some trace info and then do the normal stuff ** recap go back to code of remove-1st-trace show how it works discuss entering and leaving ** debugging (if time) have them find as many errors as they can in this, in groups --------------- (DEFINE buggy-remove-1st ; TYPE: (-> (T (list T)) (list T)) (LAMBDA (item ls) ; ENSURES: result is same as ls, ; except the first occurrence ; of item is removed (COND [(null? ls) '()] [((equal? car(ls) item)) car(ls)] [else (cons (car ls) remove-1st(ls) '())]))) --------------- first look for syntax errors if scheme doesn't even know what you're saying, ... Common syntax errors: parens in wrong places car(ls) vs (car ls) too many parens ((equal? ...)) vs (equal? ...) second look for type errors: number of arguments types of arguments put in writeln statements ----------- ; ... (begin (writeln "got here") (writeln "value of y is " y) (cons 'x (cdr y))) ; ... --------- there is usually debugging stuff in a Scheme too, but this kind of debugging works for any Scheme and any language! *** other uses for begin --------- (load "/home/cs227/lib/hw2.ss") (define testit (lambda () (begin (load "buggy-remove-1st.ss") (buggy-remove-1st 'b '(a b c)) (buggy-remove-1st 'a '(a b c)) ))) --------- ** tracing built in to Chez Scheme If time show how this works too.