6.001 Tutorial 1, Spring 2004 Introduction: (5min) Me: bcr@mit.edu PhD student, computer vision Other bits: music, sports, outdoors Students: Name? Year? Major? Programming background? Favorite thing to do? Favorite snack? Take photo of class holding up sign of their name Logistics: (3min) Deadlines: PSET 1 - Due Tuesday midnight Lecture 3 - Due Wednesday 9am Project 1 - Due Friday ** Don't try to do psets 2 hours before they are due (10-midnight ** Tuesday), there is only one server and it bogs down Questions? Philosophy of the course: * What is this course about? How to think. * Scheme: * Why scheme? It's got VERY simple syntax - compare C++ Very mathematical -- based on lambda calculus Can focus using language, not learning language Basis of Python Some real systems built with Scheme/LISP: yahoo! store, airline scheduling, AI * Syntax: (case insensitive) * Self-evaluating: e.g. 5 => 5 17 => 17 "foo" => "foo" * Combination: (procedure argument argument ...) Evaluation Rule: 1) evaluate subexpressions in any order 2) APPLY value of proc to value of arguments Application Rule: 1) if primitive procedure, then just do it! 2) if compound procedure, then evalute the body of the procedure with each formal parameter replaced by the corresponding actual argument value. e.g. (+ 1 2) (+ 1 (+ 2 3)) (mystery-box (something 7)) Terminology: evaluation -- Give value to some input application - substitution model parameters arguments expression value printed representation Special Forms - exceptions to the rule Define: (define name value-exp) Evaluation rule: 1) evaluate value-exp 2) entry in table [Terminology: bind, binding] define is NOT = like in C Used to CREATE bindings (entries in the table) Only convenience that it also replaces an existing binding Only allowed at the top level, or in a lambda body (more on this later) (define x 3) (define y (+ 1 2)) contents of table? Lambda: (my favorite! -- can do almost all the rest with lambda) (lambda param-list body) param-list: (var var var) body: scheme expressions (can be more than one, value of last is returned) [Terminology: variable, name, procedure] e.g. (lambda (x) (+ x 1)) (lambda () 1 2) (lambda (x) (define y 3) (+ x y)) Evaluation rule: 1) Create a double-bubble. (draw picture) DO NOT EVALUATE BODY (only evaluated when procedure applied!) ; Syntactic sugar: (define (foo bar) (+ bar 1)) (define foo (lambda (bar) (+ bar 1))) See the pattern? ; If: (if test consequent alternative) ; does consequent if test evaluates to NOT #f. (if 0 1 2) ; 1 (if + 1 2) ; 1 ; Problems: (+ 3 4 6) 13 (- 1) -1 (/ 5) .2 (define x 3) (= x 3) #t (lambda (x) (+ x 1)) proc ; Preface: 2 types of examples: easy and hard.. guess which this is: ((lambda (x y) (y x)) (lambda () 3) (lambda (z) (+ (z) 3))) ;Value: 6 ; Walk through step by step.. it isn't hard when you follow rules ; Fantasy football example: ; touchdown: 6pts ; reception yards: 20 yards per point ; fumbles: -1 pts ; fumbles lost: -2 pts (define fantasy (lambda (t r f fl) ((+ (* 6 t) (/ r 20) (* f -1) (* fl -2))))) ; Randy Moss' stats: tds=1; rec yds=150; fumbles=0; fumbles lost=0 (fantasy 1 150 0 0) f(x) = m * x + b (lambda (x) (+ (* m x) b)) Eg: (lambda (x) (+ (* 2 x) 3)) Teaser: (define line-maker (lambda (m b) (lambda (x) (+ (* m x) b)))) (define f (line-maker 1 0)) (f 0) (f 5) Teaser: Why does if need to be a special form? (define true (lambda (first second) first)) (define false (lambda (first second) second)) (define my-if (lambda (test tb fb) (test tb fb))) ; (can do substitution model to figure out) ; Where does it fail? (my-if true 1 (/ 1 0)) ; Questions? (if time)