210 likes | 333 Vues
This lecture by Xiao Jia explores the evolution of programming languages, focusing on the functional paradigm. Key concepts include high-order functions, static and dynamic scoping, and language implementations like LISP and Scheme. The session covers the definitions of fundamental operations like `car` and `cdr`, as well as the construction of language systems using LISP. Participants will also learn about lazy evaluation in programming, factorial computation using actors, and the role of function composition in languages like Standard ML. This comprehensive overview highlights the development of programming paradigms that enhance computational efficiency.
E N D
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia xjia@cs.sjtu.edu.cn The Evolution of PLs
The FunctionalParadigm The Evolution of PLs
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 The Evolution of PLs
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 The Evolution of PLs
It’s straightforward to build languages and systems “on top of” LISP • (LISP is often used in this way) The Evolution of PLs
Lambda • f = λx.x2 (lambda (x) (* x x)) • ((lambda (x) (* x x)) 4) 16 The Evolution of PLs
Dynamic Scoping Static Scoping int x = 4; f() { printf(“%d”, x); } main() { int x = 7; f(); } Dynamic Scoping Describe a situation in which dynamic scoping is useful The Evolution of PLs
Interpretation Defining car (cond ((eq (car expr) ’car) (car (cadrexpr)) ) … The Evolution of PLs
Scheme • corrects some errors of LISP • both simpler and more consistent (define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) The Evolution of PLs
Factorial with actors (define actorial (alpha (n c) (if (= n 0) (c 1) (actorial (- n 1) (alpha (f) (c (* f n))))))) The Evolution of PLs
Static Scoping (define n 4) (define f (lambda () n)) (define n 7) (f) LISP: 7 Scheme: 4 The Evolution of PLs
Example: Differentiating The Evolution of PLs
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 The Evolution of PLs
SASL • St. Andrew’s Symbolic Language The Evolution of PLs
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 The Evolution of PLs
Lazy Evaluation if x = 0 then 1 else 1/x In C: X&&Y ifXthenYelsefalse X||Y ifXthentrueelseY if (p != NULL && p->f > 0) … The Evolution of PLs
Standard ML (SML) • MetaLanguage The Evolution of PLs
Function Composition - infix o; - fun (f o g) x = g (f x); val o = fn : (’a -> ’b) * (’b -> ’c) -> ’a -> ’c - val quad = sqosq; val quad = fn : real -> real - quad 3.0; val it = 81.0 : real The Evolution of PLs
List Generator infix --; fun (m -- n) = if m < n then m :: (m+1 -- n) else []; 1 -- 5 [1,2,3,4,5] : int list The Evolution of PLs
Sum & Products fun sum [] = 0 | sum (x::xs) = x + sum xs; fun prod [] = 1 | prod (x::xs) = x * prod xs; sum (1 -- 5); 15 : int prod (1 -- 5); 120 : int The Evolution of PLs
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); The Evolution of PLs