Download presentation
Presentation is loading. Please wait.
Published byEsther Copeland Modified over 8 years ago
1
Mercer Institute for Personal Robots in Education (IPRE) CSC 170 Computing: Science and Creativity
2
Mercer 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 PROGRAMMING PARADIGMS 2
3
Mercer 3
4
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 Examples (though not pure) Scheme, Haskell, Snap! FUNCTIONAL PROGRAMMING (REVIEW) en.wikipedia.org/wiki/Functional_programming f(x)=(x+3)* x + x 3 * x f x
5
Mercer 5
6
“Sequential” Programming Computation a series of steps 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 IMPERATIVE PROGRAMMING f(x)=(x+3)* x
7
Mercer 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? 1 EXAMPLE USING 2 DIFFERENT STYLES
8
Mercer 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 FUNCTIONAL STYLE
9
Mercer 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 THIS DOES THE SAME, IN A DIFFERENT STYLE
10
Mercer 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 WHY FUNCTIONAL?
11
Mercer 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) WHY IMPERATIVE?
12
Mercer Which of the following is true? a)Functional style code is actually imperative style code b)Imperative style code is actually functional style code c)Both a) and b) d)Neither a) or b) QUESTION
13
Mercer 13
14
Mercer 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
15
Mercer 15 MESSAGES Objects exhibit many effects on each other These interactions are represented in software as “messages” Think of using a block
16
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
17
Mercer ONE TYPE CAN GENERATES MANY OBJECTS One class makes many instances (objects) Each object has its own values and identity
18
Mercer OOP IN SNAP Two instances of class counter Each object has its own values and identity
19
Mercer 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 OOP EXAMPLE: SKETCHPAD
20
Mercer 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 WHY OO?
21
Mercer 21
22
Mercer 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 DECLARATIVE PROGRAMMING
23
Mercer 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 QUERY A DATA BASE WITH SQL
24
Mercer 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 WHY DECLARATIVE?
25
Mercer 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! MOST LANGUAGES ARE HYBRIDS!
26
Mercer 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 WAYS TO REMEMBER THE PARADIGMS www.cs.aau.dk/~normark/prog3- 03/html/notes/paradigms_themes-paradigm-overview- section.html
27
Mercer Each paradigm has its unique benefits I f 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 SUMMARY
28
Mercer Dan Garcia Rick Mercer ATTRIBUTION
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.