cs 332 programming language conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · –...

21
February 7, 2017 Sam Siewert CS 332 Programming Language Concepts Lecture 5 – Programming Language Control Flow & Programming Paradigms

Upload: others

Post on 13-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

February 7, 2017 Sam Siewert

CS 332Programming Language Concepts

Lecture 5 – Programming Language Control Flow & Programming

Paradigms

Page 2: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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

Page 3: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

Expression EvaluationOperator Precedence

Just Parenthesize Well!

C has 15 levels

Sam Siewert 5

Page 6: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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

Page 11: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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/

Page 12: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

Most Widely Used / Popular PLshttp://redmonk.com/sogrady/2014/01/22/language-rankings-1-14/

Sam Siewert 16

Page 17: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

Domain Specialized PLshttps://www.sra.com/scl/ - Spacecraft Command Language

Neuroscript – Specific ANN Configuration and Implementation

Sam Siewert 17

Page 18: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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 Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · – Assignment #6 – Final Report on PL Compare or Custom PL Design – FINAL EXAM – Presentation

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