CS152-week2-1x - Department of Computer Science

Download Report

Transcript CS152-week2-1x - Department of Computer Science

CS152
Programming Paradigms
Thaddeus Aid
Department of Computer Science
San Jose State University
Spring 2016
Creative Commons Attribution-ShareAlike 4.0 International License
1
A Quick Political Aside
• I have already mentioned that the point of computer science is to fix
problems and change the world.
•
•
•
•
Technology and education are highly impacted by politics
Science is highly impacted by politics
Voter turnout in 2012 – 56%
Voter turnout in 2014 – 36%
• This means that if you show up your voice is worth 2-3x what it should be
• Your tuition fees, etc depend on the outcome of the election
• Young voters out number old voters
• Get registered and vote!!!!
• http://www.rockthevote.com
Creative Commons Attribution-ShareAlike 4.0 International
License
2
Adds – Good News
• Professor Khuri (Chair of CS) has approved increasing class size
• This may mean moving class rooms
• See me during break for add code
Creative Commons Attribution-ShareAlike 4.0 International
License
3
TotalBoox.com
• The MLK library offers San Jose City Library card to all students
• Take in proof of address to obtain card
• The San Jose City Library system offers card holders TotalBoox.com
memberships
• TotalBoox.com gives free access to a LARGE number of technical
ebooks (as well as normal library books)
• Please make time to sign up, I will be using these books for reading
material.
• http://www.totalboox.com/
Creative Commons Attribution-ShareAlike 4.0 International
License
4
Citation
• This lecture written using
http://www.cse.chalmers.se/~bernardy/pp/Lectures.html#sec-1 as a
source.
Creative Commons Attribution-ShareAlike 4.0 International
License
5
What is a Paradigm?
• Definition: A philosophical and theoretical framework of a scientific
school or discipline within which theories, laws, and generalizations
and the experiments performed in support of them are formulated;
broadly: a philosophical or theoretical framework of any kind.
• Why bother?
Creative Commons Attribution-ShareAlike 4.0 International
License
6
Programming Paradigms
• Tools to help organize thoughts about solving problems
• What are you thinking about while writing a program?
•
•
•
•
•
What the problem is
The sequence of actions to solve the problem
How to break the problem in to easy to solve sub-problems
What resources/agents do I need to solve the problem
What data do we need?
Creative Commons Attribution-ShareAlike 4.0 International
License
7
Paradigms as Virtual Computers
• Not every machine is optimal for every problem
• Paradigms tend to derive from how we think about computers
•
•
•
•
•
•
•
Silicon wafers + transistors?
Memory + Instructions?
Rewriting Engine?
Mathematical Function evaluation machine?
A network?
A theorem tester?
What else?
Creative Commons Attribution-ShareAlike 4.0 International
License
8
Language as a Shaper of Thought
• "a language that doesn't affect the way you think about
programming, is not worth knowing.“ ~ Alan Perlis
• Any multi-lingual students?
• How does the language you speak affect the way you think?
• Computer Languages encourage different forms of thought.
Creative Commons Attribution-ShareAlike 4.0 International
License
9
How Programming Languages Affect Thinking
• Each language has a different set up for its tools
• Though Turing-Complete languages can solve any computable problem some
are better at certain tasks than others
• The API Libraries contain different functionality
• They make some implementation methods easier than others
• There is generally a “path of least resistance”
• It is easy to write procedural code in C but not functional or object oriented
• Procedural code is harder to write in Java but Java can almost handle
functional programming.
Creative Commons Attribution-ShareAlike 4.0 International
License
10
Creative Commons Attribution-ShareAlike 4.0 International
License
11
A View of Paradigms
• http://www.cse.chalmers.se/~bernardy/pp/paradigmsDIAGRAMeng.
pdf
Creative Commons Attribution-ShareAlike 4.0 International
License
12
Important Features of a Programming
Language
• Some of the Important Features are:
•
•
•
•
•
•
•
•
•
Data Structure/Records
Procedure
Recursion
Naming/Abstraction
Memory/State
Process
Communication
Unification
Search
Creative Commons Attribution-ShareAlike 4.0 International
License
13
A Reason Paradigms are Useful
• Have you ever read someone else’s code?
•
•
•
•
Was it a mess?
Did it make sense?
Did it follow predicable methods and uses of APIs?
Was it easy to understand and change?
Creative Commons Attribution-ShareAlike 4.0 International
License
14
Patterns
• There is a paradigm shift happening now
• Patterns
• As times goes one you will find that certain ways are more effective to solve
the same problem
• These get codified (formally and informally) as patterns
• You can get a leg up on implementation using patterns
Creative Commons Attribution-ShareAlike 4.0 International
License
15
Abstraction and Types
• Why use types?
• It allows us to quickly identify the type of data we are dealing with
• Typical types
•
•
•
•
•
•
•
•
Integer
Long
Float
Double
Char
String
Boolean
Rational
Creative Commons Attribution-ShareAlike 4.0 International
License
16
Types (Cont)
• Types are typically referred to:
• someValue : itsType
•
•
•
•
•
•
•
0 : Int
1 : Int
False : Boolean
3.14 : Float
‘w’ : Char
“Hello World!” : String
Etc.
• Types are implemented differently in different languages
• Even differently in the same language depending on hardware platform
Creative Commons Attribution-ShareAlike 4.0 International
License
17
Types (Cont)
• Do we need all these types?
• What other types do we need?
Creative Commons Attribution-ShareAlike 4.0 International
License
18
Naming
• There are two hard problems in Computer Science
• Naming, Cache Validation, and Off By One Errors
• Naming a value allows for a reduction of copy errors and a
simplification of design
Creative Commons Attribution-ShareAlike 4.0 International
License
19
Parameterization and Function Types
• Greet : String
• Greet = “Hello!”
• Name : String
• Name = “Thad”
• Greet = “Hello “ + Thad
• Function greet(name) = “Hello “ + name
• Return?
• Greet : String -> String
Creative Commons Attribution-ShareAlike 4.0 International
License
20
Imperative Paradigm
• The von Neumann computer (Memory + Instructions)
• Based on the Turing machine
• Give commands to computer
• The algorithm and the data are separate
• Problem: I am hungry.
• How do we solve it?
Creative Commons Attribution-ShareAlike 4.0 International
License
21
Imperative Toolbox
• GOTO
Creative Commons Attribution-ShareAlike 4.0 International
License
22
Imperative Toolbox
• Loops and Conditionals
Creative Commons Attribution-ShareAlike 4.0 International
License
23
Imperative Toolbox
• Procedures (Functions)
Creative Commons Attribution-ShareAlike 4.0 International
License
24
Imperative Toolbox
• Pass by Value
• Pass by Reference
• Pointers
• Addresses
• Memory Sizes/Blocks
• Memory Contents
Creative Commons Attribution-ShareAlike 4.0 International
License
25
Imperative Toolbox
• Recursion
• Fibonacci Sequence
• Factorial
Creative Commons Attribution-ShareAlike 4.0 International
License
26
Problem
• I’m Hungry
• Solve using Imperative Paradigm
Creative Commons Attribution-ShareAlike 4.0 International
License
27
In Class Exercises/Homework Exercises
• Algorithms to Build
•
•
•
•
•
•
•
Min/Max
Bubble Sort
Insert Sort
Merge Sort
Binary Search
Hamming Distance
Needleman-Wunch
Creative Commons Attribution-ShareAlike 4.0 International
License
28