CS61A Lecture 3Clicker Test REVIEW: Defining functionsA new way for defining functions! λA new way for defining functions! λSlide 6Lambda returns a procedureDefining procedures using lambda λTry it! Rewrite using λTry It!“Normal functions”Functions can return functions!Rewriting using syntactic sugarPowerPoint PresentationCalling a procedure that returns a procedureSlide 16Slide 17Solution:Functions can be data to another function!Functions as Data!When can I use different variables?Let (create new variables in definitions)Using let to define temporary variablesUnix ReviewSlide 25Try It! SOLUTIONCS61A Lecture 32011-06-22Colleen LewisClicker Test Are you coming to the potluck on Thursday on the 4th floor of Soda from 6:30pm-9:00? A)YesB)MaybeC)NoREVIEW: Defining functions (define (square x) (* x x))DefineSquare to be a procedure That takes in an argument xAnd multiplies x by xA new way for defining functions! λSTk>#[closure arglist=args 196d20]STk> squareSTk> #[closure arglist=(x) 7ff09c08]STk> + (define (square x) (* x x)) square (define square _______________Defining a procedure isn’t that different than defining a variableProcedures are “things”A new way for defining functions! λ STk>ySTk> sqSTk> #[closure arglist=(x) 7ff0c248]STk> 36(define y 100)(define sq (lambda(x)(* x x))) sq (sq 6)Special formREVIEW: Defining functions (define (square x) (* x x))DefineSquare to be a procedure That takes in an argument xAnd multiplies x by xLambda returns a procedure (define sq(lambda(x)(* x x))DefineThat takes in an argument xAnd multiplies x by xA procedureDefining procedures using lambda λ(define (sum a b c) (+ a b c) )(lambda(a b c))This version without lambda is called “syntactic-sugar”Try it! Rewrite using λ(define (average x y) (/(+ x y) 2) )(lambda(x y))A)easy B)medium C)hard D)stuckTry It!(define addTwo (lambda(y)(+ y 2)))(define (addTwo y) (+ y 2))Step 1: Rewrite addTwo with syntactic sugar!Step 2: Vote: How would you call addTwo?A) ((addTwo 3))B) (addTwo 3)C) ((addTwo) 3)D) ’(addTwo 3)E) No clue!Correct Answer“Normal functions”(define (sum-of-sq a b) (+(* a a)(* b b)))Functions can return functions!Rewriting using syntactic sugar (define add-punctuation (lambda (punctuation) (lambda (sent) (se sent punctuation))))(define (add-punctuation punctuation) (lambda (sent) (se sent punctuation)))(define add-punctuation (lambda (punctuation) (lambda (sent) (se sent punctuation))))(define (add-punctuation punctuation) (lambda (sent) (se sent punctuation)))A) Neither are functionsB) Only I is a functionC) Only II is a function D) I&II are both functionsE) Not sure I) add-punctuationII)(add-punctuation ‘?)Correct AnswerThese are equivalent!Calling a procedure that returns a procedure(define (add-punctuation punctuation) (lambda (sent) (se sent punctuation)))(define add-exclamation (add-punctuation ’!))(define add-question-mark(add-punctuation ’?))Calling a procedure that returns a procedure(define (add-punctuation punctuation) (lambda (sent) (se sent punctuation)))(define add-exclamation (add-punctuation ’!))>(add-exclamation ’(the potluck will be awesome))(the potluck will be awesome !)(define (a b) (lambda (c d) (lambda (e f) ’(hello))))STk> ____ a ___________________How many open parentheses should go before a to get the sentence (hello) returned?A)1 B) 2 C) 3 D)4 E)??Try to fill in the blanks on both side!Solution:(define (a b) (lambda (c d) (lambda (e f) ’(hello))))(a 1)((a 1) 2 3)(((a 1) 2 3) 4 5)Returns a procedure that takes args c & dReturns a procedure that takes args e & fFunctions can be data to another function!Functions as Data!(define (addTwo n) (+ n 2))(define (addFour n) (addTwo (addTwo n))(define (call-twice funct x))(funct (funct x)))STk>(call-twice addTwo 20)24When can I use different variables?• A “global” variable, similarly, can be used anywhere:STk>(define pi 3.1415926535) STk>(define (cat) ‘(meow meow meow))•Arguments to procedures can be used inside that procedureSTk> (define (square x) (* x x))Let (create new variables in definitions)(let ((variable1 value1) ;;definition 1 (variable2 value2) ;;definition 2 ) statement1 ;;body statement2 ... )Using let to define temporary variables•let lets you define variables within a procedure:(define (scramble-523 wd) (let ((second (first (bf wd))) (third (first (bf (bf wd)))) (fifth (item 5 wd))) (word fifth second third) ) )(scramble-523 'meaty) yeaUnix Review•ls ______________________•cd folder1 ______________________•cd .. ______________________•cd ______________________•mkdir folder2 ______________________•rm file1 ______________________•emacs file1 & ______________________Unix Review•ls ______________________•cd folder1 ______________________•cd .. ______________________•cd ______________________•mkdir folder2 ______________________•rm file1 ______________________•emacs file1 & ______________________List contents of folderDouble click on folderGo UP one folder levelGo to home/main folder Create new folderRemove somethingCreate file in current folderTry It! SOLUTION(define addTwo (lambda(y)(+ y 2)))(define (addTwo y) (+ y 2))Step 1: Rewrite addTwo with syntactic sugar!Step 2: Vote: How would you call addTwo?A) ((addTwo 3))B) (addTwo 3)C) ((addTwo) 3)D) ’(addTwo 3)E) No clue!Correct
View Full Document