cs 332 programming language conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · –...
TRANSCRIPT
February 7, 2017 Sam Siewert
CS 332Programming Language Concepts
Lecture 5 – Programming Language Control Flow & Programming
Paradigms
Reminders…Quiz #1
Changing Direction to Look at PL Design
Assignment #1 Grading
Turn in Assignment #2
Start Assignment #3 Next Week and Complete before Exam #1
Remaining Assignments– Assignement #3 – Experience with Data Transformation (Data Flow)– Assignment #4 – Last Practice Exercise [Subroutines]– Assignment #5 – Programming Language Paradigm Compare – Proposal
for Final Effort– Assignment #6 – Final Report on PL Compare or Custom PL Design– FINAL EXAM – Presentation of PL Compare or Custom PL Design
Sam Siewert 2
Control Flow1. Sequencing (basic block)2. Selection (if-then-else, switch, case)3. Iteration (do while, while, for)4. Procedural Abstraction (void foo(void);)5. Recursion (eratosrecursive.cpp)6. Concurrency (pthread.c)7. Exception Handling and Speculation (try
and catch, exception.cpp)8. Non-determinacy (e.g. fairness over time
and time-slicing)
Sam Siewert 3
Humorous Aside …The word foo originated as a nonsense word from the 1930s, the military term FUBAR emerged in the 1940s, and the use of foo in a programming context is generally credited to the Tech Model Railroad Club (TMRC) of MIT from circa 1960.[4] However, the precise relationship of these terms is not known with certainty, and several anecdotal theories have been advanced to identify them.
The etymology of foo is explored in the Internet Engineering Task Force (IETF) RFC 3092, which gives the earliest documented use as being in the 1930s comic Smokey Stover by Bill Holman, where it is used as a nonsense word.[5][6] Holman states that he used the word due to having seen it on bottom of a jade Chinese figurine in Chinatown, San Francisco, meaning "good luck".[7][8][9] This is presumably as a transliteration of the fu character (fú, 福), which is a common character for fortune, and figurines of the trio of eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities; compare Fu Manchu, fictional character popular in the 1930s. Smokey Stover ran 1935–73, and continued to feature foo prominently, as on the front of the "foomobile" illustrated in the cover at right. The word foo became very popular in the 1930s, and also appeared in other cartoons including the Looney Tunescartoons of Bob Clampett such as The Daffy Doc and Porky in Wackyland (both 1938, with Daffy Duck and Porky Pig), and in other comic strips such as Pogo.
From there, the Foo migrated into military slang, merged with "FU" of the FUBAR.[2] The term foo fighter was used by Allied aircraft pilots in World War II to describe various UFOs or mysterious aerial phenomena.
The first known use of the terms in print in a programming context appears in a 1965 edition of MIT's "Tech Engineering News".[10]
Sam Siewert 4
http://en.wikipedia.org/wiki/Foobar
Expression EvaluationOperator Precedence
Just Parenthesize Well!
C has 15 levels
Sam Siewert 5
Short Circuit LogicEfficient, But Can Impact Testing and Test Coverage and Does not Mix Well with Side Effects
If ((a < b) && (b < c)) // short-circuit AND, a >= b
If ((a < b) || (b < c)) // short-circuit OR, a < b
If ((a < compute_b(&x)) && (compute_b(&y) < c)) // short-circuit AND, a >= compute_b(), but safe???
Side Effects?Path Coverage, Statement Coverage?Be Careful with Globals and Side Effects
Sam Siewert 6
Side Effects
Most critical in the context of functionsSide effect is permanent state change caused by execution of function– some global effect of call other than return value– assignment statements provide the ultimate example of side
effects– they change the value of a variable (i.e. state of memory)
Short Circuits are a Case Where DangerousMany more Dangerous Situations – E.g. Concurrency?
Sam Siewert 7
Outlaw Side Effects?
Several languages outlaw side effects for functions– easier to prove properties of programs– closer to Mathematical intuition– easier to optimize– (often) easier to understand
But side effects can be nice– consider rand()
Sam Siewert 8
Sequencing, Iteration, SelectionThe Core of Imperative Languages along with Subroutines (Imperative Procedural)
C Conditional Expression?Iteration, Selection, Sequencing – sharpen.cConcurrency – sharpen_grid.c
E.g. Convolution Function, PSF– Pixel’s new value is k/8 multiplied by each neighbor +
(k+1)*Pixel[i,j]– Boundary Conditions?– Wrap Around or Extend? – Extend With Interpolation
Sam Siewert 9
Review of Convolution Conventions{P} – Originally Sampled Frame or Sub-frame of Pixels{Q} – First Transformation of {P}– E.g. Negative Image is {Q}=Saturation – {P}, Where Saturation
for 8-bit Gray-level is 255– E.g. a Difference Image is {R} = {Q} – {P} is the time of sample of
{Q} is greater than {P}– A threshold
P0 is the Pixel of Interest in a Neighborhood– So, for all pixels in {P}, if Q0=P1, then {Q} is the same image
shifted one pixel to the left– If {Q}={P}+beta, modifies brightness– If {Q}={P} x gamma, modifies contrast– If {Q}={P} x gamma + beta, modifies both contrast and
brightness– This Can Define a PSF for an Image Convolution – e.g. Sharpen
Sam Siewert 10
Common PSFsSee www.dspguide.com/CH24.PDF– For all pixels, do {Q0 =
P0*h0+P1*h1+P2*h2+P3*h3+P4*h4+P5*h5+P6*h6+P7*h7+P8*h8; }
Convolution of causes no change to image Sam Siewert 11
http://www.dspguide.com/
Iteration vs. RecursionAlgorithms Can Be Cast Into Either ImplementationControl Logic vs. Stack Context
Sieve of Eratosthenes (Prime Finder) – with 2 Approaches1. eratositerator.cpp2. eratosrecursive.cpp
Very Intuitive for Divide and Conquer and Tree Traversal
Inherently Recursive Languages Need Great Stack Management
Thread Safety! – Avoid Side Effects
Sam Siewert 12
Recursion
Tail recursion– No computation follows recursive call
int gcd (int a, int b) {
/* assume a, b > 0 */
if (a == b) return a;
else if (a > b) return gcd (a - b,b);
else return gcd (a, b – a);
}
Uses Stack, So Be Careful not to Overflow
Sam Siewert 13
Course GoalsThe course includes study of the theoretical foundations to design and implement modern programming languages, including syntax, type systems, semantics, and memory structures. Comparison of several programming languages in different paradigms such as procedural, functional, logic, and scripting languages. Programming assignments will be given in each language studied
Java and C/C++ - Imperative Procedural / Object Oriented
Scripted and Functional or Declarative
Sam Siewert 14
Comparison of LanguagesRosetta Code
http://rosettacode.org/wiki/Greatest_common_divisor
Thoughts on Side Effects?
Recursion?
Imperative vs. Functional?
Sam Siewert 15
Most Widely Used / Popular PLshttp://redmonk.com/sogrady/2014/01/22/language-rankings-1-14/
Sam Siewert 16
Domain Specialized PLshttps://www.sra.com/scl/ - Spacecraft Command Language
Neuroscript – Specific ANN Configuration and Implementation
Sam Siewert 17
Recent Change in Q3-2014?
Sam Siewert 18
Most Common PLs:C/C++, Python, PHP,Java, JavaScript
AI Projects and ATP, Functional ProgrammingLisp Family Tree of PLs
Scheme, Prolog[New AI and ATP]
DBMS Big DataEngineering
TestAutomation[Ruby, Perl,
Shell, Python]
Mobile
First OOP
EmbeddedC/C++ & ASM
code.google.com/p/go/Google “Go” PL
More Coding
Mor
e Ta
lk
HPC
Not Much Talk, SignificantCode Development
More Talk thanCode Development
FamouslyCryptic
Facing Extinction or New?
www.processing.org
IT CMpuppetlabs.com
Editors[Vi and Emacs]
Family Tree of Programming Languages
E.g. C (Imperative Procedural)
E.g. Lisp Influenced (Functional), Python, Lisp, Ruby, Halide
E.g. C++, Java (Object Oriented)
Sam Siewert 19
Activity – Does Proliferation of PLs Assist?
Pick One of the Three – 3 Minutes, Present
Discussion – Does the Proliferation of PLs Assist with:1. Software Engineering – Building the Right Application
Correctly?2. Computer Science – Computability, Data Structures and
Algorithms?3. Specific Fields of Study?
Sam Siewert 20
Assignment # 3 - Compare Two Languages and/or Methods
PSF Sharpen - Speed of Execution – 120 kpixel to 12 mpixel– Cactus-120kpixel.ppm– Cactus-12mpixel.ppm
Recursion, Iteration, Concurrency (Threads, Tasks)
Time it!ssiewert@ssiewert-VirtualBox:~/a331/lcm$ time ./lcmcC gcd(146, 18)=2C lcm(146, 18)=1314
real 0m0.046suser 0m0.000ssys 0m0.000s
Count Instructions/Steps for One Pixel Transformation (Using Debugger if Needed)Compare Readability
Sam Siewert 21