CS153, Class 12 Variable-Arity Procedures Overview: * Some terminology * Common variable-arity procedures * Defining your own variable-arity procedures * Documenting your own variable-arity procedures * Lab Notes: * Thanks for coming to class on a sub-zero day. * Sorry about Wednesday's class. + Daniel is all better, but the others seem to be getting sick. + Are there questions on the material from that class? * Are there questions on the latest lab writeup? + Can you use things you've learned after that lab? Certainly. + What about really long error strings? See this coming Monday's reading. * Expect exam 1 (take-home) next Friday ---------------------------------------- Terminology: * Procedure definitions and procedure calls (define square (lambda (val) (* val val))) (square 5) What is val relative to definition of square? * Argument * Parameter What is 5 relative to call to square? * User input variable? * Argument * Parameter Some computer scientists: * The thing in the definition is a "parameter" * The thing in the use is an "argument" Other computer scientists * The thing in the definition is a "formal parameter" * The thing in the use is an "actual parameter" Lazy computer scientsts * "formal" * "actual" Term: "Arity": The number of actual parameters a procedure expects Traditional +: Arity of 2 Cons: Arity of 2 Arity of list-ref: 2 Arity of car: 1 Arity of + in Scheme: Varies depending on use (0 or more) List: 0 or more Append: 0 or more -: At least 1 ARity of 1: Unary Arity of 2: Binary Variable-arity: Variable-arity Procedure with zero or more parameters (lambda val body) * The parameters end up getting encapsulated into a list (define zebra (lambda val val)) > (zebra 'black 'white 'black 'white) ('black 'white 'black 'white) What about "one or more parameters"? (lambda (firstval . remaining-vals) body) * firstval is a single value * remainig-vals is a list Two or more parameters (lambda (firstval secondval . remaining-vals) body) ---------------------------------------- REFLECTION * Lots of shorthands for nested cars/cdrs + (cadr val) is shorthand for (car (cdr val)) + (caar val) is shorthand for (car (car val)) + (cdar val) is shorthand for (cdr (car val)) + etc. + cadaar * Favorite comments in your code ; display-list is a keeper. I've moved it to globals.scm ; All of my predictions were correct. I'm on a roll. * It's ALL notational shorthand for (lambda params body) + Implement (lambda (val) body) as (lambda params (cond ((null? params) (error "Expects one parameter")) ((not (null? (cdr params))) (error "Expects one parameter")) (else (let ((val (car params))) body)))) + Implement (lambda (val . vals) body) as (lambda params (cond ((null? params) (error "Expects one or more parameters")) (else (let ((val (car params)) (vals (cdr params))) body)))) + ETC. * How should you define call-arity? (define call-arity (lambda params (letrec ((kernel (lambda (lst) (if (null? lst) 0 (+ 1 (kernel (cdr lst))))))) (kernel params)))) vs. (define call-arity (lambda params (length params))) * Here's an interesting incorrect variant. What's wrong with it? (define call-arity (lambda params (if (null? params) 0 (+ 1 (call-arity (cdr params))))))