11 - Programming languages

55
Programming languages www.tudorgirba.com

description

I used this set of slides for the lecture on Programming Languages I gave at the University of Zurich for the 1st year students following the course of Formale Grundlagen der Informatik.

Transcript of 11 - Programming languages

Page 1: 11 - Programming languages

Programminglanguages

www.tudorgirba.com

Page 2: 11 - Programming languages

computerinformation information

computation

Page 3: 11 - Programming languages

A language is a set of sequences of symbols that we interpret to attribute meaning.

Page 4: 11 - Programming languages

Example

All elements of an array a of length N are either zero or one

∀i: 0≤i<N: ( ai=0 ∨ ai=1 )

(∀i: 0≤i<N: ai=0) ∨ (∀i: 0≤i<N: ai=1)

(1)

(2)

ambiguous

Page 5: 11 - Programming languages

A programming language is a a language for communicating software designs.

Page 6: 11 - Programming languages
Page 7: 11 - Programming languages
Page 8: 11 - Programming languages
Page 9: 11 - Programming languages
Page 10: 11 - Programming languages

programming = modeling

Page 11: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)

Page 12: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)statements

Page 13: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)statements

expression

Page 14: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)statements

variables

expression

Page 15: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)statements

variables

literals

expression

Page 16: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)statements

controlconstructs

variables

literals

expression

Page 17: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)statements

controlconstructs

functions

variables

literals

expression

Page 18: 11 - Programming languages

# Compute factorialsdef fact(n) if n == 0 1 else n * fact(n-1) endend

puts fact(ARGV[0].to_i)statements

controlconstructs

comments functions

variables

literals

expression

Page 19: 11 - Programming languages

Alan Turing, 1937

Page 20: 11 - Programming languages

Smalltalk Java==

Page 21: 11 - Programming languages
Page 22: 11 - Programming languages

programming = modeling

Page 23: 11 - Programming languages
Page 24: 11 - Programming languages

Imperativedata + algorithms

Page 25: 11 - Programming languages

Imperativedata + algorithms

Object-orientedobjects + messages

Page 26: 11 - Programming languages

Imperativedata + algorithms

Functionalstateless + pure functions

Object-orientedobjects + messages

Page 27: 11 - Programming languages

Imperativedata + algorithms

Functionalstateless + pure functions

Object-orientedobjects + messages

Logicfacts + rules

Page 28: 11 - Programming languages

Jacquard loom1801

Page 29: 11 - Programming languages

Babbage’sAnalyticalEngine1837

Page 30: 11 - Programming languages

(λ x. (λ y. x)) a b

(λ y. a) b

a

Church’sLambda Calculus 1932

Page 31: 11 - Programming languages

Touring Machine1936

Page 32: 11 - Programming languages

HarvardMark I1944

Page 33: 11 - Programming languages

Subroutine1949

Page 34: 11 - Programming languages

Assembly code1950s

Page 35: 11 - Programming languages

C AREA OF A TRIANGLE - HERON'S FORMULAC INPUT - CARD READER UNIT 5, INTEGER INPUTC OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUTC INPUT ERROR DISPAY ERROR OUTPUT CODE 1 IN JOB CONTROL LISTING INTEGER A,B,C READ(5,501) A,B,C 501 FORMAT(3I5) IF(A.EQ.0 .OR. B.EQ.0 .OR. C.EQ.0) STOP 1 S = (A + B + C) / 2.0 AREA = SQRT( S * (S - A) * (S - B) * (S - C)) WRITE(6,601) A,B,C,AREA 601 FORMAT(4H A= ,I5,5H B= ,I5,5H C= ,I5,8H AREA= ,F10.2,12HSQUARE UNITS) STOP END

Fortran1955

Page 36: 11 - Programming languages

ALGOL1958

begin ...end

<statement> ::= <unconditional statement> | <conditional statement> | <for statement>...

f() ... f()

Page 37: 11 - Programming languages

Lisp1958

(defun factorial (n) (if (= n 1) 1 (* n (factorial (- n 1)))))

Garbage collection

Programs = Data

Page 38: 11 - Programming languages

COBOL1959

ADD YEARS TO AGE.MULTIPLY PRICE BY QUANTITY GIVING COST.SUBTRACT DISCOUNT FROM COST GIVING FINAL-COST.

Modules

Page 39: 11 - Programming languages

Basic1964

10 INPUT "What is your name: ", U$20 PRINT "Hello "; U$30 INPUT "How many stars do you want: ", N40 S$ = ""50 FOR I = 1 TO N60 S$ = S$ + "*"70 NEXT I80 PRINT S$90 INPUT "Do you want more stars? ", A$100 IF LEN(A$) = 0 THEN GOTO 90110 A$ = LEFT$(A$, 1)120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30130 PRINT "Goodbye "; U$140 END

Page 40: 11 - Programming languages

JCL1964

//IS198CPY JOB (IS198T30500),'COPY JOB',CLASS=L,MSGCLASS=X//COPY01 EXEC PGM=IEBGENER//SYSPRINT DD SYSOUT=*//SYSUT1 DD DSN=OLDFILE,DISP=SHR//SYSUT2 DD DSN=NEWFILE,// DISP=(NEW,CATLG,DELETE),// SPACE=(CYL,(40,5),RLSE),// DCB=(LRECL=115,BLKSIZE=1150)//SYSIN DD DUMMY

Page 41: 11 - Programming languages

Semaphores1965

P1 — acquire resource X … critical sectionV1 — release resource X

P2 — acquire resource X … critical sectionV2 — release resource X

Page 42: 11 - Programming languages

Planner1969

man(socrates).mortal(X) :- man(X).

Prolog1972

Page 43: 11 - Programming languages

Planner1969

man(socrates).mortal(X) :- man(X).

?- mortal(socrates).Yes

Prolog1972

Page 44: 11 - Programming languages

Planner1969

man(socrates).mortal(X) :- man(X).

?- mortal(socrates).Yes

Prolog1972

?- mortal(elvis).No

Page 45: 11 - Programming languages

Pascal1970

function gcd (a, b: integer) : result real;var x : integer;begin if b= 0 then gcd := a else begin x := a; while (x >= b) do begin x := x - b end; gcd := gcd(b,x) endend

Page 46: 11 - Programming languages

C1972

#include <stdio.h>//echo the command line argumentsint main (int argc, char* argv[]) { int i; for (i=1; i<argc; i++) { printf("%s ", argv[i]); } printf("\n"); return 0;}

Page 47: 11 - Programming languages

Smalltalk1972

Integer»factorial self = 0 ifTrue: [^ 1]. self > 0 ifTrue: [^ self * (self - 1) factorial]. self error: 'Not valid for negative integers'

Everything is an ObjectEverything happens by sending messages

5 factorial -> 120

Page 48: 11 - Programming languages

Monitors1974

public class Account { protected int assets = 0; ... public synchronized void withdraw(int amount) { while (amount > assets) { try { wait(); } catch(InterruptedException e) { } } assets -= amount; } ...}

Page 49: 11 - Programming languages

Bourne shell1977

cat Notes.txt| tr -c '[:alpha:]' '\012'| sed '/^$/d’| sort| uniq –c| sort –rn| head -5

Page 50: 11 - Programming languages

SQL1978

SELECT * FROM Book WHERE price > 100.00 ORDER BY title;

Page 51: 11 - Programming languages

Perl1978

CGI1993

#!/usr/bin/perl -wprint "Content-type: text/html\n\n";print <<'eof'<html><head><title>Directory contents</title></head><body><h1>Directory contents</h1><ul>eof;@files = <*>;foreach $file (@files) { print '<li><a href="' . $file . '">' . $file . "</li>\n";}print "</ul></body></html>\n";__END__

Page 52: 11 - Programming languages

JavaScript1995

Ajax2005

Page 53: 11 - Programming languages
Page 54: 11 - Programming languages

programming = modeling