click it out - university of california, berkeleycs10/su13/lec/05/l04... · 2013. 6. 27. · uc...

22
UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (1) Garcia a) T b) i c) r d) e Click it out byob.berkeley.edu

Upload: others

Post on 08-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (1)

Garcia

a)  T b)  i c)  r d)  e

Click it out … byob.berkeley.edu!

Page 2: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (2)

Garcia

a)  T b)  i c)  r d)  e

Click it out … byob.berkeley.edu!

Page 3: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (3)

Garcia

Is this a function?

a)  Yes b)  No

Click it out … byob.berkeley.edu!

Page 4: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

The Beauty and Joy of Computing

Lecture #4

Programming Paradigms

HAND-HELD DEVICE BRIDGES LANGUAGE GAP In a growingly diverse population, like the US, where more and more people have English as a second language, how can technology help?

http://www.npr.org/blogs/alltechconsidered/2013/06/27/186525030/translation-please-hand-held-device-bridges-language-gap!

Quest (first exam) in 12 days!!

Page 5: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (5)

Garcia

§  What are they? ú  Most are Hybrids!

§  The Four Primary ones ú  Functional ú  Imperative ú  Object-Oriented

   OOP Example: Skecthpad

ú  Declarative §  Turing Completeness §  Summary

Programming Paradigms Lecture

Page 6: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (6)

Garcia

§  “The concepts and abstractions used to represent the elements of a program (e.g., objects, functions, variables, constraints, etc.) and the steps that compose a computation (assignation, evaluation, continuations, data flows, etc.).”

§  Or, a way to classify the style of programming.

What are Programming Paradigms? en.wikipedia.org/wiki/Programming_paradigm!

Page 7: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (7)

Garcia

§  This makes it hard to teach to students, because most languages have facets of several paradigms! ú  Called “Multi-paradigm”

languages ú  Scratch too!

§  It’s like giving someone a juice drink (with many fruit in it) and asking to taste just one fruit!

Most Languages Are Hybrids!

Page 8: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (8)

Garcia

§  Computation is the evaluation of functions ú  Plugging pipes together ú  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

§  Examples (not purely) ú  Scheme, Scratch, BYOB

Functional Programming (review) en.wikipedia.org/wiki/Functional_programming!

f(x)=(x+3)* x!

+!x 3!

*!

x!

f x!

Page 9: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (9)

Garcia

§  “Sequential” Programming §  Computation a series of steps

ú  Assignment allowed    Setting variables

ú  Mutation allowed    Changing variables

§  Like following a recipe. E.g., §  Procedure f(x)

ú  ans = x ú  ans = ans ú  ans = (x+3) * ans ú  return ans

§  Examples: (not purely) ú  Pascal, C

Imperative Programming en.wikipedia.org/wiki/Imperative_programming!

f(x)=(x+3)* x!

Page 10: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (10)

Garcia

a)  Functional Style b)  Imperative Style

Click it out … byob.berkeley.edu!

Page 11: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (11)

Garcia

a)  Functional Style b)  Imperative Style

Click it out … byob.berkeley.edu!

Page 12: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12)

Garcia

§  Objects as data structures ú  With methods you ask of them

   These are the behaviors

ú  With local state, to remember    These are the attributes

§  Classes & Instances ú  Instance an example of class ú  E.g., Fluffy is instance of Dog

§  Inheritance saves code ú  Hierarchical classes ú  E.g., pianist special case of

musician, a special case of performer

§  Examples (not purely) ú  Java, C++

Object-Oriented Programming (OOP) en.wikipedia.org/wiki/Object-oriented_programming!

www3.ntu.edu.sg/home/ehchua/programming/java/images/OOP-Objects.gif!

Page 13: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (13)

Garcia

§  Dr. Ivan Sutherland ú  “Father of Computer

Graphics” ú  1988 Turing Award

(“Nobel prize” for CS) ú  Wrote Sketchpad for his

foundational 1963 thesis

§  The most impressive software ever written

§  First… ú  Object-oriented system ú  Graphical user interface ú  non-procedural language

OOP Example : SketchPad en.wikipedia.org/wiki/Sketchpad!

Spent the past few years doing

research @ Berkeley in EECS dept!

Page 14: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (14)

Garcia

OOP in BYOB Consider energy consumption … Predict the output?"

Message Passing …"

Page 15: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (15)

Garcia

§  Express what computation desired without specifying how it carries it out ú  Often a series of

assertions and queries ú  Feels like magic!

§  Sub-categories ú  Logic ú  Constraint

   We saw in Sketchpad!

§  Example: Prolog

Declarative Programming en.wikipedia.org/wiki/Declarative_programming!

Anders Hejlsberg “The Future of C#” @ PDC2008

channel9.msdn.com/pdc2008/TL16/

Page 16: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (16)

Garcia

Declarative might look like … Consider energy consumption"

Page 17: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (17)

Garcia

§  Five schoolgirls sat for an examination. Their parents – so they thought – showed an undue degree of interest in the result. They therefore agreed that, in writing home about the examination, each girl should make one true statement and one untrue one. The following are the relevant passages from their letters:

§  Betty ú  Kitty was 2nd ú  I was 3rd

§  Ethel ú  I was on top ú  Joan was 2nd

§  Joan ú  I was 3rd ú  Ethel was last

§  Kitty ú  I came out 2nd ú  Mary was only 4th

§  Mary ú  I was 4th ú  Betty was 1st

Declarative Programming Example

Page 18: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (18)

Garcia

a)  Functional b)  Imperative c)  OOP d)  Declarative e)  All equally powerful

Of 4 paradigms, what’s the most powerful?

Page 19: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (19)

Garcia

a)  1 (functional) b)  1 (not functional) c)  2 d)  3 e)  4

Of 4 paradigms, how many can BYOB be? byob.berkeley.edu!

Page 20: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (20)

Garcia

§  A Turing Machine has an infinite tape of 1s and 0s and instructions that say whether to move the tape left, right, read, or write it ú  Can simulate any computer

algorithm!

§  A Universal Turing Machine is one that can simulate a Turing machine on any input

§  A language is considered Turing Complete if it can simulate a Universal Turing Machine ú  A way to decide that one

programming language or paradigm is just as powerful as another

Turing Completeness en.wikipedia.org/wiki/Turing_completeness

ironphoenix.org/tril/tm/!

Turing Machine by Tom Dunne

Xkcd comic “Candy Button Paper”

Page 21: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (21)

Garcia

§  Functional ú  Evaluate an

expression and use the resulting value for something

§  Imperative ú  First do this

and next do that

§  Object-oriented ú  Class and instances

   House Example ú  Send messages

between objects to simulate the temporal evolution of a set of real world phenomena

§  Declarative ú  Answer a question via

search for a solution

Ways to Remember the Paradigms

www.cs.aau.dk/~normark/prog3-03/html/notes/paradigms_themes-paradigm-overview-section.html

en.wikipedia.org/wiki/Programming_paradigm

Page 22: Click it out - University of California, Berkeleycs10/su13/lec/05/L04... · 2013. 6. 27. · UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (12) Garcia

UC Berkeley “The Beauty and Joy of Computing” : Programming Paradigms (22)

Garcia

§  Each paradigm has its unique benefits ú  If a language is Turing

complete, it is equally powerful

ú  Paradigms vary in efficiency, scalability, overhead, fun, “how” vs “what” to specify, etc.

§  Modern languages usually take the best from all ú  E.g., Scratch

   Can be functional    Can be imperative    Can be object-oriented    Can be declarative

Summary