the beauty and joy of the beauty and joy of computing
play

The Beauty and Joy of The Beauty and Joy of Computing Computing - PowerPoint PPT Presentation

The Beauty and Joy of The Beauty and Joy of Computing Computing Lecture #4 Lectur e #4 Functions Functions UC Berkeley EECS UC Berkeley EECS Sr Lectur Sr Lecturer SOE er SOE Dan Garcia Dan Gar cia The success of Apples Siri (only


  1. The Beauty and Joy of The Beauty and Joy of Computing Computing Lecture #4 Lectur e #4 Functions Functions UC Berkeley EECS UC Berkeley EECS Sr Lectur Sr Lecturer SOE er SOE Dan Garcia Dan Gar cia The success of Apple’s Siri (only available on the iPhone 4S) has sparked competition, to be sure. Google’s IRIS (Siri spelled backward), and now Evi (available on BOTH iOS and Android). The popularity has meant the servers are down (they didn’t use Cloud storage clearly – we’ll learn about that later). Love where this is going! www.technologyreview.com/computing/39560/ �

  2. Generalization (in CS10) REVIEW § You are going to learn to write functions, like in math class: x y = sin(x) sin ú sin is the function ú x is the input “Function machine” from Simply Scheme (Harvey) ú It returns a single value, a number Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (2) (2)

  3. Function basics § Functions take in 0 or more inputs and return exactly 1 output § The same inputs MUST yield same outputs. ú Output function of input only § Other rules of functions ú No state (prior history) ú No mutation (no variables get modified) CS Illustrated function metaphor ú No side effects (nothing else happens) Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (3) (3)

  4. More Terminology (from Math) § Domain § Range ú The “class” of input a function ú All the possible return values accepts of a function § Examples § Examples ú Sqrt of ú Sqrt of  Positive numbers  Non-negative numbers ú Length of ú Length of  Sentence, word, number  Non-negative integer ú _ < _ ú _ < _  Both: Sentence, word, number  Boolean (true or false) ú _ and _ ú _ and _  Booleans  Boolean (true or false) ú Letter _ of _ ú Letter _ of _  Number from 1 to input length  Letter  Sentence, word, number Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (4) (4)

  5. Types of input (there are more) • Words separated by N Sentences spaces, N ≥ 0 • E.g., CS 10 is great • Length ≥ 1, no spaces Word • Cal, 42, CS10 • Length = 1 Character • E.g., A, 3, # • 0-9 only Digit • E.g., 7 Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (5) (5)

  6. Why functions are great! § If a function only depends on the information it gets as input, then nothing else can affect the output. ú It can run on any computer and get the same answer. § This makes it incredibly easy to parallelize functions. ú Functional programming is a Datacenter great model for writing software that runs on multiple systems at the same time. Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (6) (6)

  7. Scratch à BYOB (Build Your Own Blocks) § Scratch § BYOB (to be “S NAP !”) ú Invented @ MIT ú Based on Scratch code ú Maintained by MIT ú Maintained by jens & Cal ú Huge community ú Growing community ú No sharing (yet) L ú Sharing via Website ú No functions L ú Functions! J … “Blocks” ú Scratch 2.0 in Flash ú BYOB 4.0 in HTML5  No iOS devices. L  All devices J ú scratch.mit.edu ú byob.berkeley.edu Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (7) (7)

  8. Why use functions? (1) The power of generalization! Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” UC Berkeley “The Beauty and Joy of Computing” : Functions : Functions (8) (8)

  9. Why use functions? (2) They can be composed together to make even more magnificent things. They are literally the building blocks of almost everything that we create when we program. We call the process of breaking big problems down into smaller tasks functional decomposition Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (9) (9)

  10. Types of Blocks Ty § Command ú No outputs, meant for side-effects § Reporter (Function) ú Any type of output § Predicate (Function) ú Boolean output  (true or false) Gar Garcia cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (10) (10)

  11. Quick Preview: Recursion Recursion is a M. C. Escher : Drawing Hands � technique for defining functions that use themselves to complete their own definition. We will spend a lot of time on this. Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (11) (11)

  12. en.wikipedia.org/wiki/Functional_programming Functional Programming Summary § Computation is the f(x)=(x+3)* x evaluation of functions ú Plugging pipes together x ú Each pipe, or function, has exactly 1 output ú Functions can be input! x � x 3 § Features ú No state + f  E.g., variable assignments ú No mutation  E.g., changing variable values ú No side effects * § Need BYOB not Scratch Garcia Gar cia UC Berkeley “The Beauty and Joy of Computing” : Functions UC Berkeley “The Beauty and Joy of Computing” : Functions (12) (12)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend