programming paradigm - University of Arizona

Download Report

Transcript programming paradigm - University of Arizona

CSC 170
Computing: Science and Creativity
BIG IDEAS: CREATIVITY, ABSTRACTION, DATA AND INFORMATION, ALGORITHMS
PROGRAMMING PARADIGMS
Institute for Personal Robots in Education
(IPRE)
Mercer
PROGRAMMING PARADIGMS
• A programming paradigm is a fundamental style of computer
programming, serving as a way of building the structure and
elements of computer programs.
http://en.wikipedia.org/wiki/Programming_paradigm
• A way to describe a style of programming
• The four primary paradigms
• Functional
• Imperative
• Object-Oriented
• Declarative
Mercer
2
Mercer
3
en.wikipedia.org/wiki/Functional_programming
FUNCTIONAL PROGRAMMING (REVIEW)
• Computation is the evaluation of
functions
• Plugging functions together
• Each has exactly 1 output (report)
• Functions can be input!
• Features
• No state
• E.g., variable assignments
• No mutation
• E.g., changing variable values
• No side effects
f(x)=(x+3)* x
x
x
x 3
+
f
*
• Examples (though not pure)
• Scheme, Haskell, Snap!
Mercer
Mercer
5
IMPERATIVE PROGRAMMING
• “Sequential” Programming
• Computation a series of steps
f(x)=(x+3)* x
• Assignment allowed
• Setting variables
• Mutation allowed
• Changing variables
• Like following a recipe
• Procedure f(x)
•
•
•
•
ans = x
ans = ans
ans = (x+3) * ans
return ans
• Examples: Pascal, C, Java
Mercer
1 EXAMPLE USING 2 DIFFERENT STYLES
• Consider two styles for creating a list of the even integers in a list
• The imperative style is to loop through the elements of the input list
and add the evens to a new list. What is the Output?
Mercer
FUNCTIONAL STYLE
• There are no assignments
• Uses reporter blocks to build up lists
• Report a new list that extends another list with a new item at the front
without affecting the original list
• Report the value of the item at the specified place in the list
• Report all but the first item of the list
Code Demo the same block in a functional style
Mercer
THIS DOES THE SAME, IN A DIFFERENT STYLE
• Instead of a loop, the block calls itself
• Each time, the list argument is a smaller list, until it’s empty
• Then the evens (item 1s) are reported to the block to build the new list
Mercer
WHY FUNCTIONAL?
• Started as an academic language
• cool, recursion, no loops, no assignment, difficult to learn
• Because there is no assignment, can run on different processors
• Allows programs to run on many processors
• Allows program to run on many computers
• Tell story of talking to Microsoft at my conference: Azure: 300 to 3,000 nodes, sequence
and analyze genomes TV ad
Mercer
WHY IMPERATIVE?
• Common sense: sequence the things that need to be done
• Conveniently captures algorithmic patterns such sequence,
selection, and repetition
• Easier to read and program
• More fun
• Can still encapsulate algorithms as a function (block)
Mercer
QUESTION
• Which of the following is true?
a)
b)
c)
d)
Functional style code is actually imperative style code
Imperative style code is actually functional style code
Both a) and b)
Neither a) or b)
Mercer
Mercer
13
OBJECT-ORIENTED PROGRAMMING (OOP)
• Objects put together data and functions
that need that data
• Consider how we would model a sprite
that can be animated and have different
costumes
• Must ma intain attributes (data) about
• location, list of customs, size, transparency, direction …
• Has behaviors:
•
•
•
•
Move from current location to another location
Change size, transparency, direction, costume, …
Say things
Hide and show
Mercer
MESSAGES
• Objects exhibit many effects on each other
• These interactions are represented in software as “messages”
• Think of using a block
15
Mercer
ENCAPSULATION
• Often need many of the same objects
• Numbers, Strings, PokerHands, Strategies, Employees …
• Need an efficient way to redefine the same thing many times
• and hide the details from other objects
• Each object (instance) has its own state — the set of values for
that instance
• A block can define the behaviors and attributes
Mercer
ONE TYPE CAN GENERATES MANY OBJECTS
•
One class makes
many instances
(objects)
• Each object has its
own values and
identity
Mercer
OOP IN SNAP
• Two instances of class counter
• Each object has its own values and identity
Mercer
OOP EXAMPLE: SKETCHPAD
• 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-imperative language
https://www.youtube.com/watch?v=495nCzxM9
PI
Mercer
WHY OO?
• Easier to model the real world and program
• What was 10,000 lines of code
• Became 1,000 functions with 10 lines of code
• Became 100 classes with 10 functions
• The little idea that became huge:
• Combine functions and the data they need to produce information
into a structure
Mercer
Mercer
21
DECLARATIVE PROGRAMMING
• A style of building the structure
and elements of computer
programs, that expresses the
logic of a computation without
describing its control flow
• Express what computation
desired without specifying how it
carries it out
• Often a series of queries
• Example: SQL, Prolog
Mercer
QUERY A DATA BASE WITH SQL
• Example: Return a list of books and the number of authors
associated with each
SELECT Book.title AS Title, COUNT(*) AS Authors
FROM Book
JOIN Book_author
ON Book.isbn = Book_author.isbn
GROUP BY Book.title;
• Example output might resemble the following:
Title
Authors
Snap Examples and Guide 4
The Joy of Snap
1
An Introduction to Snap 2
Pitfalls of Snap
1
Mercer
WHY DECLARATIVE?
• Many application involve lots of data, such things to buy on
Ebay,
• Can generate information such as all vehicles for sale in the
range of 8k to 10k in a 100 mile radius
Mercer
MOST LANGUAGES ARE HYBRIDS!
• This makes it hard to teach to
students, because most
languages have facets of several
paradigms!
• Called “Multi-paradigm” languages
• Snap! too
• It’s like giving someone a juice
drink (with many fruit in it) and
asking to taste just one fruit!
Mercer
WAYS TO REMEMBER THE PARADIGMS
• Functional
• Evaluate an expression and
use the resulting value for
something
• Imperative
• First do this
and next do that
• Object-oriented
• 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
www.cs.aau.dk/~normark/prog303/html/notes/paradigms_themes-paradigm-overviewsection.html
Mercer
SUMMARY
• 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., Snap!
• Can be functional
• Can be imperative
• Can be object-oriented
• Can be declarative
Mercer
ATTRIBUTION
Dan Garcia
Rick Mercer
Mercer