Presentation201213
Download
Report
Transcript Presentation201213
Comp3202 Languages and Paradigms
Proposal 1 All humans possess a common
logical structure which operates independently of
language
Proposal 2 Language determines what the
individual perceives in the world and how he
thinks about it.
The Whorfian Hypothesis
“A language that doesn’t affect the way you think
about programming, is not worth knowing”
(Dikstra)
What languages do we know ?
Forth
Prolog
Logo
FORTRAN
ALGOL
Pascal
Python
PL/1
C#
C
COBOL
Lisp
Scheme
Basic
Assembler
Ruby
Perl
Oak
Java
C++
Programming Paradigms
Lang
Lang
Lang
Lang
Lang
Lang
Paradigm
Lang
Lang
Paradigm
Paradigm
Lang
Paradigm
Lang
Lang
Lang
Programming Paradigms
What is a “Paradigm” ?
Important
Paradigms
• Imperative (procedural)
• Declarative
• Functional
• Logic
• Object Oriented
• Event Driven
Imperative Paradigm
E.W.Dijkstra
//
//
//
//
//
//
//
//
//
HLSL Code for bending of a Free-Free Euler-Bernoulli Beam
CBP 18-08-12
Parameter inputs are k, DoB,B(time dependent),xLocn,Len
MatEd inputs wp (World Position)
Here B can be dynamically updated via UScript to effect the motion
of the bending plate.
float3 outp;
float3 x;
Imperative Code
float fn;
// scale the x value to 0 .. 1
x = (wp.x - xLocn + Len/2)/Len;
// calc the beam displacement
//fn = B*(cos(k*x) - cosh(k*x)) + B*DoB*(sin(k*x) - sinh(k*x));
fn = B*(cos(k*x) + cosh(k*x)) + B*DoB*(sin(k*x) + sinh(k*x));
// Note that z is down so we must invert our calculation...
outp.z = -fn;
outp.x=0;
outp.y=0;
return outp;
S1
?
S2
S1
Sn
?
S1
S2
?
S1
?
S1
Imperative vs. Declarative (Functional)
float x;
(define (square a) (x a a))
float function square(float a) {
float b;
b = a x a;
return b;
}
x = square(3);
(square 3)
Scheme : Definitions and Expressions
Scheme : Functions
(define (timesBySeven x)
(* x 7))
Scheme : Functions
(define (sum x y)
(+ x y))
Scheme : Conditionals (1)
(define (test x)
(cond ( (> x 0)1)
( (= x 0)0)
( (< x 0)-1)))
Scheme : Conditionals (2)
(define (test2 x)
(if (< x 0)(- x)
x))
Recursion
Recursion
See Recursion
On page 269 in the index of Kernighan and Richie’s book
The C Programming language
recursion
86,139,141,182,202,269
Recursion in Language: 5thC BC Panini Sanskrit Grammar Rules
20thC Chomsky theorizes that unlimited extension of English is possible
through the use of recursion.
… try Googling “Recursion”
Scheme : Recursion : The Factorial Function (1)
How many ways can you get n people to sit in n chairs ?
n = 3. (A,B,C)
n = 4. (A,B,C,D)
Scheme : Recursion
sum the numbers 1 to N
(e,g, 1 to 4)
(define (sum n)
(if (= n 1)1
(+ n (sum(- n 1)))))
(sum 4) =
4 + (sum 3) =
Scheme : Recursion (2) The Factorial Function
(define (fact n)
(if (= n 1)1
(* n (fact (- n 1)))))
Functional Recursion // Imperative Iteration
(define (fact n)
(if (= n 1)1
(* n (fact (- n 1)))))
Fundamental Principles of Recursion
A recursive function must call a simplified version of itself.
This is guaranteed to “bottom out” (to halt). Any call to itself
would produce an infinite regress.
Don’t run this … ah go on then ..
(define (fact n)
(if (= n 1)1
(* n (fact n))))
... and wait for the error message ... or worse
Recursion is not Self-Reference
A recursive function makes reference to a simplified version
of itself: (I am) better than what (I was)
I’m the humblest person I know
I never make misteaks
“I never make predictions. I never have and I never will”
This sentence contains five words
This sentence no verb
Computational Biology
Lindenmayer Systems
Axiom: F
Rule : F -> F[-F]F[+F][F]
Compilers - Parsing
A
(
A
)
+
A
var
Compilers - Parsing
A
(
A
)
+
A
var
Compilers - Parsing
A
(
A
)
+
A
var
Compilers - Parsing
A
(
A
)
+
A
var
Statement
var
=
expression
Expression
+
term
-
+
term
-
Term
var
num
(
expression
)
Statement
var
=
expression
Expression
+
term
-
+
term
-
Term
var
num
(
expression
)
Logic Paradigm: Prolog = Facts + Rules
Logic Paradigm: Prolog = Facts + Rules
lectures(colin,3063).
lectures(colin,3079).
lectures(pete,3078).
lectures(richard,3062).
lectures(richard,3040).
studies(tom,3063).
studies(tom,3062).
studies(kate,3063).
studies(kate,3040).
studies(kate,3062).
Prolog Queries
?- lectures(colin,M).
?- lectures(colin,M),studies(kate,M).
?- lectures(colin,M),studies(X,M).
Prolog Rules
burns(X) :- wooden(X).
wooden(X) :- floats(X).
witch(X) :- burns(X),female(X).
Monty Python Holy Grail
witch(X) :- burns(X),female(X).
burns(X) :- wooden(X).
wooden(X) :- floats(X).
wooden(woodBridge).
stone(stoneBridge).
floats(bread).
floats(apple).
floats(cherry).
floats(X) :- sameweight(duck, X).
female(girl).
sameweight(duck,girl).
[trace] 3 ?- witch(girl).
Call: (7) witch(girl) ? creep
Call: (8) burns(girl) ? creep
Call: (9) wooden(girl) ? creep
Call: (10) floats(girl) ? creep
Call: (11) sameweight(duck, girl) ?
creep
Exit: (11) sameweight(duck, girl) ?
creep
Exit: (10) floats(girl) ? creep
Exit: (9) wooden(girl) ? creep
Exit: (8) burns(girl) ? creep
Call: (8) female(girl) ? creep
Exit: (8) female(girl) ? creep
Exit: (7) witch(girl) ? creep
Yes
Coda
It is easier to write an incorrect program than to read a
correct one.
There are two ways to write error-free programs, only the
third one works.
It is easier to change the specification to fit the program than
vice-versa
Why did the Roman Empire collapse? What is the Latin for
“office automation” ?