CS 332 Programming...
Transcript of CS 332 Programming...
![Page 1: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/1.jpg)
February 3, 2020 Sam Siewert
CS 332Programming Language Concepts
Lecture 5 – Programming Language Control Flow & Programming
Paradigms
![Page 2: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/2.jpg)
Reminders…Quiz #1 - On Canvas
Exam #1, After completion of Assignment #3
Changing Direction to Look at PL Comparison/Design after Exam #1
Assignment #1 Grading Completed on Canvas
Assignment #2 Due Friday
Remaining Assignments– Assignment #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
![Page 3: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/3.jpg)
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
![Page 4: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/4.jpg)
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
![Page 5: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/5.jpg)
Expression EvaluationOperator Precedence
Just Parenthesize Well!
C has 15 levels
Sam Siewert 5
![Page 6: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/6.jpg)
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
![Page 7: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/7.jpg)
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
![Page 8: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/8.jpg)
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
![Page 9: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/9.jpg)
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
![Page 10: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/10.jpg)
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
P4 P3 P2P5 P0 P1P6 P7 P8
![Page 11: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/11.jpg)
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
0 0 00 1 00 0 0
http://www.dspguide.com/
![Page 12: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/12.jpg)
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
![Page 13: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/13.jpg)
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
![Page 14: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/14.jpg)
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
![Page 15: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/15.jpg)
Comparison of LanguagesRosetta Code
http://rosettacode.org/wiki/Greatest_common_divisor
Thoughts on Side Effects?
Recursion?
Imperative vs. Functional?
Sam Siewert 15
![Page 16: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/16.jpg)
Most Widely Used / Popular PLshttp://redmonk.com/sogrady/2014/01/22/language-rankings-1-14/
Sam Siewert 16
![Page 17: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/17.jpg)
Domain Specialized PLshttps://www.sra.com/scl/ - Spacecraft Command Language
Neuroscript – Specific ANN Configuration and Implementation
Sam Siewert 17
![Page 18: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/18.jpg)
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]
![Page 19: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/19.jpg)
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
![Page 20: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/20.jpg)
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
![Page 21: CS 332 Programming Languagesmercury.pr.erau.edu/~siewerts/cs332/documents/Lectures/Lecture-Week-5-1.pdf · eponymous male "star gods" Fú, Lù, Shòu are common in Chinese communities;](https://reader030.fdocuments.in/reader030/viewer/2022041021/5ed155eaefd7b2537304c96f/html5/thumbnails/21.jpg)
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