2013-09-16-CS10-L04-..

advertisement
The Beauty and Joy of
Computing
Quest (first exam) in in 9
days!!
Lecture #4 : Creativity &
Abstraction
UC Berkeley
EECS
Sr Lecturer SOE
Dan Garcia
FOO
baz
BAR
biz
www.technologyreview.com/news/518591/copy-protection-for3-d-printing-aims-to-prevent-a-piracy-plague/
Creativity
 “Creativity and computing are
prominent forces in innovation;
the innovations enabled by
computing have had and will
continue to have far-reaching
impact.
 At the same time,
computing facilitates exploration
and the creation of knowledge.
 This course will emphasize these
creative aspects of computing.
 Students in this course will
create interesting & relevant
artifacts with the tools and
techniques of computer science. “
UC Berkeley “The Beauty and Joy of Computing” : Functions (2)
Garcia
Computing enables people to create digitally
 Creating…
 knowledge
 tools
 expressions of ideas
 solutions to problems.
 Creating digitally…
 requires understanding and
using software tools.
 can be done by…
 combining and modifying
existing artifacts
 by creating new artifacts.
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (3)
Computing enables people to translate intention
into computational artifacts.
 A computational artifact
is created by human
conception using
software tools.
 Examples of
computational artifacts
include
 digital music
 videos
 images
 documents
 combinations of these.
E.g.,
 infographics
 presentations
 web pages.
UC Berkeley “The Beauty and Joy of Computing” : Functions (4)
Garcia
Collaboration is an essential part of creating
computational artifacts.
 Collaboration
facilitates multiple
perspectives in
developing
computational
artifacts.
 A computational
artifact can reflect
collaborative intent.
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (5)
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (6)
Dan’s kid’s 2nd grade HW!
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (7)
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)
 No side effects (nothing
else happens)
CS Illustrated function metaphor
UC Berkeley “The Beauty and Joy of Computing” : Functions (8)
Garcia
Which is NOT a function?
a)
b)
c)
d)
e)
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (9)
More Terminology (from Math)
 Domain
 The “class” of input a
function accepts
 Examples
 Range
 All the possible return
values of a function
 Examples
 Sqrt of
 Positive numbers
 Sqrt of
 Non-negative numbers
 Length of
 Sentence, word, number
 Length of
 Non-negative integer
 _<_
 Both: Sentence, word,
number
 _<_
 Boolean (true or false)
 _ and _
 Booleans
 _ and _
 Boolean (true or false)
 Letter _ of _
 Letter _ of _
 Letter
 Number from 1 to input
length
UC Berkeley
Beauty and Joy of Computing” : Functions (10)
 Sentence,
word,“The
number
Garcia
Types of input (there are more)
Sentences
Word
Character
Digit
• Words separated by
N spaces, N ≥ 0
• E.g., CS 10 is great
• Length ≥ 1, no spaces
• Cal, 42, CS10
• Length = 1
• E.g., A, 3, #
• 0-9 only
• E.g., 7
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (11)
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
great model for writing
software that runs on multiple
systems at the same time.
Datacenter
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (12)
Scratch  BYOB (Build Your Own
Blocks)
 Scratch
 BYOB (and SNAP! )
 Invented @ MIT
 Based on Scratch code
 Maintained by MIT
 Maintained by jens & Cal
 Huge community
 Growing community
 Sharing via Website
 No sharing (yet) 
 No functions 
 Functions!  … “Blocks”
 Scratch 2.0 in Flash
 Snap! Is in HTML5
 No iOS devices. 
 scratch.mit.edu
 All devices 
 snap.berkeley.edu/run
UC Berkeley “The Beauty and Joy of Computing” : Functions (13)
Garcia
Why use functions? (1)
The power of generalization!
UC Berkeley “The Beauty and Joy of Computing” : Functions (14)
Garcia
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
UC Berkeley “The Beauty and Joy of Computing” : Functions (15)
Types of Blocks
 Command
 No outputs, meant for
side-effects
 Not a function…
 Reporter (Function)
 Any type of output
 Predicate (Function)
 Boolean output
 (true or false)
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (16)
Quick Preview: Recursion
Recursion is a
technique for
defining functions
that use themselves
to complete their own
definition.
M. C. Escher : Drawing Hands
We will spend a lot of
time on this.
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (17)
en.wikipedia.org/wiki/Functional_programming
Functions Summary
 Computation is the
evaluation of functions
f(x)=(x+3)* x
 Plugging pipes together
x
 Each pipe, or function, has
exactly 1 output
 Functions can be input!
 Features
 No state
 E.g., variable assignments
 No mutation
 E.g., changing variable values
 No side effects
 Need BYOB/Snap!, and
x
x 3
+
f
*
not Scratch 1.x
Garcia
UC Berkeley “The Beauty and Joy of Computing” : Functions (18)
Download