Programming Languages

Lecturer: William W.Y. Hsu 2

2020/11/30

Why Do This?

• For the fun of it! • Understanding the principles makes it easier to learn new languages. • Sometimes you need different features of different languages, and if you don’t know about other languages how can you use them? • More effectively utilize the languages you already know. • For example, if you need “fine-grained” control over system memory, then you ++ would be better choice than . However, if you memory leaks are a big concern, then Java is a better choice than C++. 3

2020/11/30

Ultra-brief History of Languages

• In the beginning, ENIAC (Electronic Numerical Integrator and Computer) programmers used patch cords. • This gave them the raw power to compute trig tables. 4

11/30/2020 Important Events in History • 1940s: The first electronic computers were monstrous contraptions. ▫ Programmed in binary machine code by hand via switches and later by card readers and paper tape readers. ▫ Code is not reusable or relocatable. ▫ Computation and machine maintenance were difficult: machines had short mean-time to failure (MTTF) because vacuum tubes regularly burned out. ▫ The term “bug” originated from a bug that reportedly roamed around in a machine causing short circuits. 5

2020/11/30

Machine and Assembly Languages

• The next major revolution was machine language, which is just binary (or ). • Very quickly people realized that humans cannot write error free programs using just zeroes and ones without going insane. • Hence, came , which uses human readable abbreviations to stand for machine code. 6

11/30/2020 Machine Language Program

55 89 e5 53 83 ec 04 83 e4 f0 e8 31 00 00 00 89 ……

• Hard to maintain or write large programs • Needs an easier way to write program 7

2020/11/30

Assembly Languages

• Assembly languages were invented in the 50s’ to allow machine operations to be expressed in mnemonic abbreviations. ▫ Enables larger, reusable, and relocatable programs. ▫ Actual machine code is produced by an assembler. ▫ Early assemblers had a one-to-one correspondence between assembly and machine instructions.

• “Speedcoding”: expansion of macros into multiple machine instructions to achieve a form of higher-level programming. 8

2020/11/30

Assembly Language (example) 9

2020/11/30

Assembly Language Example

addiu sp,sp,-32 • Example MIPS assembly program to sw ra,20(sp) jal getint compute GCD. nop jal getint • Example MIPS R4000 machine code of the sw v0,28(sp) assembly program lw a0,28(sp) move v1,v0 beq a0,v0,D 27bdffd0 afbf0014 0c1002a8 00000000 slt at,v1,a0 A: beq at,zero,B 0c1002a8 afa2001c 8fa4001c nop b C 00401825 10820008 0064082a 10200003 subu a0,a0,v1 00000000 10000002 00832023 B: subu v1,v1,a0 C: bne a0,v1,A 00641823 1483fffa 0064082a 0c1002b2 slt at,v1,a0 00000000 8fbf0014 27bd0020 D: jal putint nop 03e00008 00001025 lw ra,20(sp) addiu sp,sp,32 Actual MIPS R4400 IC jr ra move v0,zero 10

2020/11/30

Higher Level Languages

• Eventually, people realized that more complex programs are very difficult to write at the level of assembly language. • So, eventually came higher level languages.

class Test { public static void main(String args[]) { int A, B, C; A=5; B=6; C=A+B; System.out.print(C); } } 11

2020/11/30

The First High-Level Programming Language

• Mid 1950s: development of (FORmula TRANslator), the arguably first higher-level language. ▫ Finally, programs could be developed that were machine independent! • Main computing activity in the 50s: solve numerical problems in science and engineering. • Other high-level languages soon followed: ▫ Algol 58 was an improvement compared to Fortran. ▫ COBOL for business computing. ▫ Lisp for symbolic computing and artifical intelligence. ▫ BASIC for "beginners“. ▫ C for systems programming. 12

2020/11/30

FORTRAN 77 Example

PROGRAM GCD • FORTRAN is still widely used for C variable names that start scientific, engineering, and numerical with problems, mainly because very good C I,J,K,L,N,M are exist. integers • In the early days skeptics wrongly C read the parameters predicted that compilers could not READ (*, *) I, J beat hand-written machine code. C loop while I!=J • FORTRAN 77 has: 10 IF I .NE. J THEN ▫ Subroutines, if-then-else, do-loops IF I .GT. J THEN ▫ Types (primitive and arrays) I = I - J ▫ Variable names are upper case and ELSE limited to 6 chars J = J - I ▫ No recursion ENDIF ▫ No structs/classes, unions GOTO 10 ▫ No dynamic allocation ENDIF ▫ No case-statements and no while-loops C write result WRITE (*, *) ’GCD =’, I END 13

2020/11/30

FORTRAN I,II,IV,77

PROGRAM AVEX INTEGER INTLST(99) • FORTRAN had a dramatic ISUM = 0 C read the length of the list impact on computing in READ (*, *) LSTLEN early days IF ((LSTLEN .GT. 0) .AND. (LSTLEN .LT. 100)) THEN • Still used for numerical C read the input in an array DO 100 ICTR = 1, LSTLEN computation READ (*, *) INTLST(ICTR) ISUM = ISUM + INTLST(ICTR) 100 CONTINUE C compute the average IAVE = ISUM / LSTLEN C write the input values > average DO 110 ICTR = 1, LSTLEN IF (INTLST(ICTR) .GT. IAVE) THEN WRITE (*, *) INTLST(ICTR) END IF 110 CONTINUE ELSE WRITE (*, *) 'ERROR IN LIST LENGTH' END IF END 14

2020/11/30

FORTRAN 90,95,HPF

PROGRAM AVEX INTEGER INT_LIST(1:99) • Major revisions INTEGER LIST_LEN, COUNTER, AVERAGE C read the length of the list ▫ Recursion READ (*, *) LISTLEN ▫ Pointers IF ((LIST_LEN > 0) .AND. (LIST_LEN < 100)) THEN ▫ Records C read the input in an array DO COUNTER = 1, LIST_LEN • New control constructs READ (*, *) INT_LIST(COUNTER) ▫ while-loop END DO C compute the average • Extensive set of array operations AVERAGE = SUM(INT_LIST(1:LIST_LEN)) / ▫ A[1:N] = B[1:N] * 1000.0 LIST_LEN C write the input values > average • HPF (High-Performance Fortran) DO COUNTER = 1, LIST_LEN IF (INT_LIST(COUNTER) > AVERAGE) THEN includes constructs for parallel WRITE (*, *) INT_LIST(COUNTER) computation END IF END DO ELSE WRITE (*, *) 'ERROR IN LIST LENGTH' END IF END 15

2020/11/30

Declarative and Imperative programming

• There are two types of programming languages: declarative and imperative. ▫ Declarative languages focus on what the computer should do. ▫ Imperative languages focus on how the computer should do something. 16

2020/11/30

Quicksort

• Quicksort sorts an array by recursively sorting “subarrays” as less than or greater than pivot values. 17

2020/11/30

Quicksort in Haskell

If input is empty return empty.

qsort [] = [] Otherwise, return a list qsort (x:xs) = qsort lt_x ++ [x] ++ qsort ge_x with all the values less where than x both “qsort”ed and lt_x = [y | y <- xs, y < x] before x and all values ge_x = [y | y <- xs, y >= x] greater than x both This junk defines lt_x “qsort”ed and after x. as all values less than x, and ge_x as all values greater than or equal to x. 18

2020/11/30 qsort( a, lo, hi ) int a[], hi, lo;{ int h, w, p, t; if (lo < hi) { w = lo; h = hi; Quicksort in C p = a[hi]; do { Find the first element larger than the while ((w < h) && (a[w] <= p)) pivot value and the last element w = w+1; smaller than the pivot value. while ((h > w) && (a[h] >= p)) h = h-1; if (w < h) { If these values are on the “wrong side” t = a[w]; of the pivot, swap them. a[w] = a[h]; a[h] = t; Repeat until no values are on the } “wrong side.” } while (w < h); t = a[w]; Swap the smallest value greater than a[w] = a[hi]; or equal to the pivot with the pivot, a[hi] = t; which is at the end of the list qsort( a, lo, w-1 ); qsort( a, w+1, hi ); Finally, recurse on the two sides. } } 19

2020/11/30

Quicksort in Python Dynamic typing system. import random

def quicksort(L): if len(L) > 1: Randomize the pivot for average case. pivot = random.randrange(len(L)) elements = L[:pivot]+L[pivot+1:] left = [element for element in elements if element < L[pivot]] right = [element for element in elements if element >= L[pivot]] return quicksort(left)+[L[pivot]]+quicksort(right) return L

Iterators within arrays.

The whole concept is functional, with writing with imperative constructs. 20

2020/11/30

Quicksort in OCaml

let rec quicksort = function | [] -> [] | x::xs -> let smaller, larger = List.partition(fun y -> y < x) xs in quicksort smaller @ (x::quicksort larger) 21

2020/11/30

Quicksort in Prolog

quicksort([X|Xs],Ys) :- partition(Xs,X,Left,Right), quicksort(Left,Ls), quicksort(Right,Rs), append(Ls,[X|Rs],Ys). quicksort([],[]).

partition([X|Xs],Y,[X|Ls],Rs) :- X <= Y, partition(Xs,Y,Ls,Rs). partition([X|Xs],Y,Ls,[X|Rs]) :- X > Y, partition(Xs,Y,Ls,Rs). partition([],Y,[],[]).

append([],Ys,Ys). append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs). 22

2020/11/30

Quicksort Comparison

• Notice how much more complex this program is in C (an imperative language) than Haskell (a declarative language). • However, without a very good , the quicksort in C will likely run faster than in Haskell! 23

2020/11/30

Types of Languages • Von Neumann languages allow for computation by focusing on manipulating data elements. • Object-oriented languages allow for computation by modeling principles as a series of semi-independent “objects” . • Scripting languages are a subset of von Neumann languages and are serve as “glue” between more robust languages in order to facilitate rapid development. 24

2020/11/30

Types of Languages • Functional languages are based on functions and recursion. • Dataflow languages focus on the flow of information between nodes. • Logic languages model programs as a series of logical statements. 25

2020/11/30

Lisp

(DEFINE (avex lis) • Lisp (LIst Processing) (filtergreater lis (/ (sum lis) (length lis))) • The original functional language ) (DEFINE (sum lis) developed by McCarthy as a (COND realization of Church's lambda ((NULL? lis) 0) calculus (ELSE (+ (CAR lis) (sum (CDR lis)))) ) • Many dialects exist, including ) Common Lisp and Scheme (DEFINE (filtergreater lis num) • Very powerful for symbolic (COND ((NULL? lis) '()) computation with lists ((> (CAR lis) num) (CONS (CAR lis) • Implicit memory management (filtergreater (CDR with garbage collection lis) num))) (ELSE (filtergreater (CDR lis) num) • Influenced functional ) programming languages (ML, ) Miranda, Haskell) 26

2020/11/30

Algol 60 comment avex program begin • The original block-structured language integer array intlist [1:99]; integer listlen, counter, sum, average; ▫ Local variables in a statement block sum := 0; • First use of Backus-Naur Form (BNF) comment read the length of the input list readint (listlen); to formally define language grammar if (listlen > 0) L (listlen < 100) then • All subsequent imperative programming begin comment read the input into an array languages are based on it for counter := 1 step 1 until listlen do • Not widely used in the US begin readint (intlist[counter]); • Unsuccessful successor Algol 68 is sum := sum + intlist[counter] large and relatively complex end; comment compute the average average := sum / listlen; comment write the input values > average for counter := 1 step 1 until listlen do if intlist[counter] > average then printint (intlist[counter]) end else printstring ("Error in input list length") end 27

2020/11/30

COBOL

IDENTIFICATION DIVISION. • Originally developed by the Department of PROGRAM-ID. EXAMPLE. Defense. • Intended for business data processing. ENVIRONMENT DIVISION. CONFIGURATION SECTION. • Extensive numerical formatting features and SOURCE-COMPUTER. IBM-370. decimal number storage. OBJECT-COMPUTER. IBM-370. • Introduced the concept of records and DATA DIVISION. nested selection statement. WORKING-STORAGE SECTION. 77 FAHR PICTURE 999. • Programs organized in divisions: 77 CENT PICTURE 999. IDENTIFICATION: Program identification

PROCEDURE DIVISION. ENVIRONMENT: Types of computers used DISPLAY 'Enter Fahrenheit ' UPON CONSOLE. DATA: Buffers, constants, work areas ACCEPT FAHR FROM CONSOLE. PROCEDURE: The processing parts COMPUTE CENT = (FAHR- 32) * 5 / 9. DISPLAY 'Celsius is ' CENT UPON CONSOLE. (program logic). GOBACK. 28

2020/11/30

BASIC

REM avex program • BASIC (Beginner’s All-Purpose DIM intlist(99) sum = 0 Symbolic Instruction Code) REM read the length of the input list INPUT listlen • Intended for interactive use IF listlen > 0 AND listlen < 100 THEN (intepreted) and easy for REM read the input into an array FOR counter = 1 TO listlen "beginners" INPUT intlist(counter) • Goals: easy to learn and use for non- sum = sum + intlist(counter) NEXT counter science students REM compute the average • Structure of early basic dialects average = sum / listlen REM write the input values > average were similar to Fortran FOR counter = 1 TO listlen IF intlist(counter) > average THEN • Classic Basic PRINT intlist(counter); • QuickBasic (see example) NEXT counter ELSE • MS is a popular dialect PRINT "Error in input list length" END IF END 29

2020/11/30

PL/I AVEX: PROCEDURE OPTIONS (MAIN); DECLARE INTLIST (1:99) FIXED; DECLARE (LISTLEN, COUNTER, SUM, AVERAGE) • Developed by IBM FIXED; SUM = 0; ▫ Intended to replace FORTRAN, /* read the input list length */ COBOL, and Algol GET LIST (LISTLEN); IF (LISTLEN > 0) & (LISTLEN < 100) THEN • Introduced exception handling DO; • First language with pointer /* read the input into an array */ DO COUNTER = 1 TO LISTLEN; data type GET LIST (INTLIST(COUNTER)); SUM = SUM + INTLIST(COUNTER); • Poorly designed, too large, too END; complex /* compute the average */ AVERAGE = SUM / LISTLEN; /* write the input values > average */ DO COUNTER = 1 TO LISTLEN; IF INTLIST(COUNTER) > AVERAGE THEN PUT LIST (INTLIST(COUNTER)); END; ELSE PUT SKIP LIST ('ERROR IN INPUT LIST LENGTH'); END AVEX; 30

2020/11/30 Ada and Ada95 with TEXT_IO; use TEXT_IO; procedure AVEX is package INT_IO is new INTEGER_IO (INTEGER); use INT_IO; type INT_LIST_TYPE is array (1..99) of INTEGER; • Originally intended to be the INT_LIST : INT_LIST_TYPE; standard language for all software LIST_LEN, SUM, AVERAGE : INTEGER; begin commissioned by the US SUM := 0; Department of Defense -- read the length of the input list GET (LIST_LEN); • Very large if (LIST_LEN > 0) and (LIST_LEN < 100) then -- read the input into an array • Elaborate support for packages, for COUNTER := 1 .. LIST_LEN loop GET (INT_LIST(COUNTER)); exception handling, generic SUM := SUM + INT_LIST(COUNTER); program units, concurrency end loop; -- compute the average • Ada 95 is a revision developed AVERAGE := SUM / LIST_LEN; under government contract by a -- write the input values > average for counter := 1 .. LIST_LEN loop team at Intermetrics, Inc. if (INT_LIST(COUNTER) > AVERAGE) then PUT (INT_LIST(COUNTER)); ▫ Adds objects, shared-memory NEW_LINE; synchronization, and several other end if features end loop; else PUT_LINE ("Error in input list length"); end if; end AVEX; 31

2020/11/30 class name Avex superclass Object instance variable names intlist "Class methods" "Create an instance" new Smalltalk-80 ^ super new "Instance methods" Developed by XEROX PARC: "Initialize" initialize first IDE with windows-based intlist <- Array new: 0 graphical user interfaces (GUIs) "Add int to list" add: n | oldintlist | The first full implementation of an oldintlist <- intlist. object-oriented language intlist <- Array new: intlist size + 1. intlist <- replaceFrom: 1 to: intlist size with: oldintlist. ^ intlist at: intlist size put: n "Calculate average" average | sum | sum <- 0. 1 to: intlist size do: [:index | sum <- sum + intlist at: index]. ^ sum // intlist size "Filter greater than average" filtergreater: n | oldintlist i | oldintlist <- intlist. i <- 1. 1 to: oldintlist size do: [:index | (oldintlist at: index) > n ifTrue: [oldintlist at: i put: (oldintlist at: index)]] intlist <- Array new: oldintlist size. intlist replaceFrom: 1 to: oldintlist size with: oldintlist 32

2020/11/30

Prolog avex(IntList, GreaterThanAveList) :- sum(IntList, Sum), • The most widely used length(IntList, ListLen), Average is Sum / ListLen, logic programming filtergreater(IntList, Average, language GreaterThanAveList). % sum(+IntList, -Sum) • Declarative: states what % recursively sums integers of IntList you want, not how to get it sum([Int | IntList], Sum) :- sum(IntList, ListSum), • Based on formal logic Sum is Int + ListSum. sum([], 0). % filtergreater(+IntList, +Int, -GreaterThanIntList) % recursively remove all integers <= Int from IntList filtergreater([AnInt | IntList], Int, [AnInt | GreaterThanIntList]) :- AnInt > Int, !, filtergreater(IntList, Int, GreaterThanIntList). filtergreater([AnInt | IntList], Int, GreaterThanIntList) :- filtergreater(IntList, Int, GreaterThanIntList). filtergreater([], Int, []). 33

2020/11/30

Pascal program avex(input, output); type intlisttype = array [1..99] of integer; • Designed by Swiss professor var intlist : intlisttype; Niklaus Wirth listlen, counter, sum, average : integer; begin • Designed for teaching "structured sum := 0; programming" (* read the length of the input list *) readln(listlen); • Small and simple if ((listlen > 0) and (listlen < 100)) then begin • Had a strong influence on (* read the input into an array *) for counter := 1 to listlen do subsequent high-level languages begin readln(intlist[counter]); Ada, ML, Modula sum := sum + intlist[counter] end; (* compute the average *) average := sum / listlen; (* write the input values > average *) for counter := 1 to listlen do if (intlist[counter] > average) then writeln(intlist[counter]) end else writeln('Error in input list length') end. 34

2020/11/30

C (ANSI C, K&R C) main() { int intlist[99], listlen, counter, sum, average; • One of the most successful sum = 0; programming languages /* read the length of the list */ scanf("%d", &listlen); • Primarily designed for if (listlen > 0 && listlen < 100) systems programming but { /* read the input into an array */ for (counter = 0; counter < listlen; counter++) more broadly used { scanf("%d", &intlist[counter]); • Powerful set of operators, sum += intlist[counter]; } but weak type checking and /* compute the average */ no dynamic semantic checks average = sum / listlen; /* write the input values > average */ for (counter = 0; counter < listlen; counter++) if (intlist[counter] > average) printf("%d\n", intlist[counter]); } else printf("Error in input list length\n"); } 35

2020/11/30

C++ main() { std::vector intlist; int listlen; • The most successful of several /* read the length of the list */ std::cin >> listlen; object-oriented successors of C if (listlen > 0 && listlen < 100) { int sum = 0; • Evolved from C and Simula 67 /* read the input into an STL vector */ • Large and complex, partly for (int counter = 0; counter < listlen; counter++) { int value; because it supports both std::cin >> value; intlist.push_back(value); procedural and object-oriented sum += value; programming } /* compute the average */ int average = sum / listlen; /* write the input values > average */ for (std::vector::const_iterator it = intlist.begin(); it != intlist.end(); ++it) if ((*it) > average) std::cout << (*it) << std::endl; } else std::cerr << "Error in input list length" << std::endl; } 36

2020/11/30

Java import java.io; class Avex • Developed by Sun Microsystems { public static void main(String args[]) throws IOException { DataInputStream in = new DataInputStream(System.in); • Based on C++, but significantly int listlen, counter, sum = 0, average; int [] intlist = int[100]; simplified // read the length of the list listlen = Integer.parseInt(in.readLine()); • Supports only object-oriented if (listlen > 0 && listlen < 100) { // read the input into an array programming for (counter = 0; counter < listlen; counter++) { intlist[counter] = • Safe language (e.g. no pointers Integer.valueOf(in.readline()).intValue(); sum += intlist[counter]; but references, strongly typed, } and implicit garbage collection) // compute the average average = sum / listlen; • Portable and machine- // write the input values > average for (counter = 0; counter < listlen; counter++) independent with Java virtual { if (intlist[counter] > average) System.out.println(intlist[counter] + "\n"); machine (JVM) } } else System.out.println("Error in input length\n"); } } 37

2020/11/30 Other Notable Languages • C# ▫ Similar to Java, but platform dependent (MS .NET) ▫ Common Language Runtime (CLR) manages objects that can be shared among the different languages in .NET • Simula 67 ▫ Based on Algol 60 ▫ Primarily designed for discrete-event simulation ▫ Introduced concept of coroutines and the class concept for data abstraction • APL ▫ Intended for interactive use ("throw-away" programming) ▫ Highly expressive functional language makes programs short, but hard to read • Scripting languages ▫ Perl, Python, Ruby, … 38

2020/11/30 Why are There so Many Programming Languages? • Evolution ▫ Design considerations: What is a good or bad programming construct? ▫ Early 70s: in which goto-based control flow was replaced by high-level constructs (e.g. while loops and case statements). ▫ Late 80s: nested block structure gave way to object-oriented structures. 39

2020/11/30 Why are There so Many Programming Languages? • Special Purposes ▫ Many languages were designed for a specific problem domain, e.g:  Scientific applications  Business applications  Artificial intelligence  Systems programming  Internet programming • Personal Preference ▫ The strength and variety of personal preference makes it unlikely that anyone will ever develop a universally accepted programming language. 40

2020/11/30 What Makes a Programming Language Successful? • Expressive Power ▫ Theoretically, all languages are equally powerful (Turing complete) ▫ Language features have a huge impact on the programmer's ability to read, write, maintain, and analyze programs ▫ Abstraction facilities enhance expressive power • Ease of Use for Novice ▫ Low learning curve and often interpreted, e.g. Basic and Logo • Ease of Implementation ▫ Runs on virtually everything, e.g. Basic, Pascal, and Java 41

2020/11/30 What Makes a Programming Language Successful? • Open Source  Freely available, e.g. Java • Excellent Compilers and Tools ▫ Fortran has extremely good compilers ▫ Supporting tools to help the programmer manage very large projects • Economics, Patronage, and Inertia ▫ Powerful sponsor: Cobol, PL/I, Ada ▫ Some languages remain widely used long after "better" alternatives 43

2020/11/30

Important to Know PL by Trend 45

2020/11/30 47

2020/11/30 48

2020/11/30

The Next 50 Programming Languages

• The following list of languages denotes #51 to #100. Since the differences are relatively small, the programming languages are only listed (in alphabetical order).

4th Dimension/4D, ABC, ActionScript, APL, Arc, AutoLISP, Bash, bc, Bourne shell, C shell, CFML, CL (OS/400), Clipper, Common Lisp, Elixir, Euphoria, F#, Forth, Haskell, Icon, IDL, Inform, Io, J, Korn shell, Ladder Logic, Maple, ML, MOO, MQL4, MUMPS, NATURAL, NXT-G, OCaml, OpenCL, OpenEdge ABL, Oz, PL/I, PostScript, PowerShell, Q, REXX, Ring, Scheme, Smalltalk, SPARK, SPSS, Standard ML, Stata, Tcl 49

2020/11/30 50

2020/11/30 52

2020/11/30

Important to Know PL by Trend 53

2020/11/30 54

2020/11/30 55

2020/11/30 56

2020/11/30 57

2020/11/30 58

2020/11/30