DOC PREVIEW
Berkeley COMPSCI 61A - Lecture Notes

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

Berkeley COMPSCI 61A - Lecture Notes

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?