the evolution of programming languages

21
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia [email protected] The Evolution of PLs 1

Upload: lucien

Post on 20-Feb-2016

23 views

Category:

Documents


0 download

DESCRIPTION

The Evolution of Programming Languages. Day 2 Lecturer: Xiao Jia [email protected]. The Functional Paradigm. High Order Functions. zeroth order: only variables and constants first order: function invocations, but results and parameters are zeroth order - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Evolution of Programming Languages

The Evolution of PLs 1

The Evolution of Programming Languages

Day 2Lecturer: Xiao [email protected]

Page 2: The Evolution of Programming Languages

The Evolution of PLs 2

The Functional Paradigm

Page 3: The Evolution of Programming Languages

The Evolution of PLs 3

High Order Functions

• zeroth order: only variables and constants• first order: function invocations, but results

and parameters are zeroth order• n-th order: results and parameters are (n-

1)-th order• high order: n >= 2

Page 4: The Evolution of Programming Languages

The Evolution of PLs 4

LISP

• f(x, y) (f x y)• a + b (+ a b)• a – b – c (- a b c)

• (cons head tail)• (car (cons head tail)) head• (cdr (cons head tail)) tail

Page 5: The Evolution of Programming Languages

The Evolution of PLs 5

• It’s straightforward to build languages and systems “on top of” LISP

• (LISP is often used in this way)

Page 6: The Evolution of Programming Languages

The Evolution of PLs 6

Lambda

• f = λx.x2 (lambda (x) (* x x))• ((lambda (x) (* x x)) 4) 16

Page 7: The Evolution of Programming Languages

The Evolution of PLs 7

Dynamic Scoping

int x = 4;f(){ printf(“%d”, x);}main(){ int x = 7; f();}

Static Scoping

Dynamic Scoping

Describe a situation in which dynamic scoping is useful

Page 8: The Evolution of Programming Languages

The Evolution of PLs 8

Interpretation

Defining car

(cond ((eq (car expr) ’car) (car (cadr expr)) )…

Page 9: The Evolution of Programming Languages

The Evolution of PLs 9

Scheme

• corrects some errors of LISP• both simpler and more consistent

(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1))))))

Page 10: The Evolution of Programming Languages

The Evolution of PLs 10

Factorial with actors

(define actorial (alpha (n c) (if (= n 0) (c 1) (actorial (- n 1) (alpha (f) (c (* f n)))))))

Page 11: The Evolution of Programming Languages

The Evolution of PLs 11

Static Scoping

(define n 4)(define f (lambda () n))(define n 7)(f)

LISP: 7Scheme: 4

Page 12: The Evolution of Programming Languages

The Evolution of PLs 12

Example: Differentiating

Page 13: The Evolution of Programming Languages

The Evolution of PLs 13

Example: Differentiating

(define derive (lambda (f dx) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx))))(define square (lambda (x) (* x x)))(define Dsq (derive sq 0.001))

-> (Dsq 3)6.001

Page 14: The Evolution of Programming Languages

The Evolution of PLs 14

SASL

• St. Andrew’s Symbolic Language

Page 15: The Evolution of Programming Languages

The Evolution of PLs 15

Lazy Evaluation

nums(n) = n::nums(n+1)

second (x::y::xs) = y

second(nums(0))= second(0::nums(1))= second(0::1::nums(2))= 1

infinite list

Page 16: The Evolution of Programming Languages

The Evolution of PLs 16

Lazy Evaluation

if x = 0 then 1 else 1/x

In C: X && Y if X then Y else false X || Y if X then true else Y

if (p != NULL && p->f > 0) …

Page 17: The Evolution of Programming Languages

The Evolution of PLs 17

Standard ML (SML)

• MetaLanguage

Page 18: The Evolution of Programming Languages

The Evolution of PLs 18

Function Composition

- infix o;

- fun (f o g) x = g (f x);val o = fn : (’a -> ’b) * (’b -> ’c) -> ’a -> ’c

- val quad = sq o sq;val quad = fn : real -> real

- quad 3.0;val it = 81.0 : real

Page 19: The Evolution of Programming Languages

The Evolution of PLs 19

List Generator

infix --;fun (m -- n) = if m < n then m :: (m+1 -- n) else [];

1 -- 5 [1,2,3,4,5] : int list

Page 20: The Evolution of Programming Languages

The Evolution of PLs 20

Sum & Products

fun sum [] = 0 | sum (x::xs) = x + sum xs;

fun prod [] = 1 | prod (x::xs) = x * prod xs;

sum (1 -- 5); 15 : intprod (1 -- 5); 120 : int

Page 21: The Evolution of Programming Languages

The Evolution of PLs 21

Declaration by cases

fun fac n = if n = 0 then 1 else n * fac(n-1);

fun fac 0 = 1 | fac n = n * fac(n-1);