Writing Functions in Scheme Writing Functions in Scheme Suppose we want a function ct which takes a list of symbols and returns the number of symbols in the list (ct (a b c)) 3 (ct ()) 0 (ct (x y z w t)) 5 How can we write this function? Answer #1: Have the instructor write it ;; ct : <list-of-sym> -> <num> ;; (ct ()) 0 ;; (ct (a b c)) 3 [else (ct (cdr l)))])) Checking My Answer: Empty List Checking My Answer: Empty List [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct ()) [(null? ()) 0] [else (ct (cdr ())))]) [(null? ()) 0] [else (ct (cdr ())))]) [#t 0] [else (ct (cdr ())))]) 1-4
Checking My Answer: Empty List [else (ct (cdr l)))])) [#t 0] [else (ct (cdr ())))]) [else (ct (cdr l)))])) 0 [else (ct (cdr l)))])) (ct (a b c)) [else (ct (cdr l)))])) [(null? (a b c)) 0] [else (ct (cdr (a b c))))]) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? (a b c)) 0] [else (ct (cdr (a b c))))]) [else (ct (cdr (a b c))))]) [else (ct (cdr (a b c))))]) (ct (cdr (a b c)))) 5-8
[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct (cdr (a b c)))) (ct (b c))) (ct (b c))) [(null? (b c)) 0] [else (ct (cdr (b c))))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? (b c)) 0] [else (ct (cdr (b c))))])) [else (ct (cdr (b c))))])) [else (ct (cdr (b c))))])) (ct (cdr (b c))))) 9-12
[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct (cdr (b c))))) (ct (c)))) (ct (c)))) [(null? (c)) 0] [else (ct (cdr (c))))]))) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? (c)) 0] [else (ct (cdr (c))))]))) [else (ct (cdr (c))))]))) [else (ct (cdr (c))))]))) (ct (cdr (c)))))) 13-16
[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct (cdr (c)))))) (ct ())))) (ct ())))) [(null? ()) 0] [else (ct (cdr ())))])))) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? ()) 0] [else (ct (cdr ())))])))) [#t 0] [else (ct (cdr ())))])))) [#t 0] [else (ct (cdr ())))])))) 0))) 17-20
[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) 0))) 1)) 1)) 2) Writing Functions in Scheme: Answer #2 [else (ct (cdr l)))])) 2) [else (ct (cdr l)))])) 3 Answer #2: Use the general design recipe Locate or write a Write a contract Write examples Create a template that follows the shape of the Convert the template to the final function Run examples as tests 21-25
Writing Functions in Scheme: Answer #2 Data Definitions Answer #2: Use the general design recipe Locate or write a Write a contract Write examples Create a template that follows the shape of the Convert the template to the final function What is a "list of symbols"? Sometimes the is given, somtimes you have to create it Usually include it in your code as a comment Run examples as tests works 90% of the time Contracts Examples A contract is a comment that identifies set of input values and output values Examples (usually in comments at first) help clarify the purpose of the function ;; ct: <list-of-sym> -> <num> All mentioned data sets should have a somewhere ;; (ct ()) 0 ;; (ct (a b c)) 3 Make sure that every case in the is covered at least once 26-34
Template Template Two cases in implies cond with two cond-lines Template Template Corresponding predicate for each data case Extract parts in cases with meta-variables 35-38
Template Template Recursive call for self-references in A template depends only on the input data; it ignores the function s purpose (Nevertheless, generating a template, which is fairly automatic, usually provides most of the function) Template to Function Template to Function Transform template to function line-by-line Transform template to function line-by-line 39-43
Template to Function Reminder: Recipe Transform template to function line-by-line [(pair? l) (ct (cdr l)) )])) Sometimes, a part of the template isn t needed Locate or write a Write a contract Write examples Create a template that follows the shape of the Convert the template to the final function Run examples as tests Reminder: Template Steps More Examples Create a cond expression with one line for each case in the Write down a predicate for each case For the answer, extract parts in cases with meta-variables For each self-reference in the, add a recursive call (more examples in class) Shape of template shape == Shape of 44-47
Generalized Recipe Locate or write s Write contracts Write examples Create a template that follows the shape of the data definition, one for each Convert the templates to the final functions Run examples as tests 48-49