Programming Paradigms

Post on 18-May-2015

2.387 views 1 download

Tags:

Transcript of Programming Paradigms

Intelligent People. Uncommon Ideas.

11

Programming Paradigms

(http://www.directi.com | http://wiki.directi.com | http://careers.directi.com)

Licensed under Creative Commons Attribution Sharealike Noncommercial

By, Janeve George janeve.g@directi.com & Nilesh Mevada nilesh.m@directi.com

22Creative Commons Sharealike Attributions Noncommercial

Few Instruction

• It's a workshop not a presentation !!!

• Parking Lot

• Perks

33Creative Commons Sharealike Attributions Noncommercial

Aim of the session

• Introduction to programming concepts and techniques

• Differentiate between various programming paradigms

Q: Why there are so many programming paradigms?A:

Q: Isn't there 'one language' that would suffice?A:

Q: How can we deal with understanding so many paradigms?A:

44Creative Commons Sharealike Attributions Noncommercial

Flow of the session

• Programming Paradigm Definition

• Why bother about 'Programming Paradigms'?

• Appreciating 'Programming paradigms'

• Programming Concepts An Overview Eager and Lazy Evaluation Procedures Functions External and Internal State Concurrency Static vs Dynamic Typing

• Some Real-Life Problems Search (Java vs Prolog) Higher Order Functions (Java vs Haskell)

55Creative Commons Sharealike Attributions Noncommercial

Flow of the session

• Programming Paradigm - Definition

• Why bother about 'Programming Paradigms'?

• Appreciating 'Programming paradigms'

• Programming Concepts An Overview Eager and Lazy Evaluation Procedures Functions External and Internal State Concurrency Static vs Dynamic Typing

• Some Real-Life Problems Search (Java vs Prolog) Higher Order Functions (Java vs Haskell)

66Creative Commons Sharealike Attributions Noncommercial

Programming Paradigm - Definition

• What are Programming Paradigms? Fundamental style of computer programming. (wikipedia.org) It serves as a pattern or model for a programming language

FunctionOriented

ObjectOriented

LogicalProgramming

Java

ML

Haskell

Prolog

Mercury Oz

Erlang

Smalltalk

77Creative Commons Sharealike Attributions Noncommercial

Flow of the session

• Programming Paradigm - Definition

• Why bother about 'Programming Paradigms'?

• Appreciating 'Programming paradigms'

• Programming Concepts An Overview Eager and Lazy Evaluation Procedures Functions External and Internal State Concurrency Static vs Dynamic Typing

• Some Real-Life Problems Search (Java vs Prolog) Higher Order Functions (Java vs Haskell)

88Creative Commons Sharealike Attributions Noncommercial

Why bother about 'Programming Paradigms'?

99Creative Commons Sharealike Attributions Noncommercial

Why bother about 'Programming Paradigms'?

Bridge Example

Requirement Better approachTools oneknows

The bridge should be- Earthquake resistant at x level- Should be erosion resistant- So on...

Pros and cons of building it with either wood or iron

One knowing pros & cons of wood, iron (& possibly concrete) to satisfy the requirements.

1010Creative Commons Sharealike Attributions Noncommercial

Why bother about 'Programming Paradigms'?

Programming world

Requirement Better approach Tools oneusually knows

General real-life problems

a set of programming concepts related to a programming paradigm

one knowing programming concepts related to multiple programming paradigms

1111Creative Commons Sharealike Attributions Noncommercial

Why bother about 'Programming Paradigms'?

• Tools (Programming Languages) may not suffice to deal with the real life problems. They might not support the solution at all or only partially.

• Learning new programming languages becomes easier

• To bring innovations in programming technologies

1212Creative Commons Sharealike Attributions Noncommercial

Flow of the session

• Programming Paradigm - Definition

• Why bother about 'Programming Paradigms'?

• Appreciating 'Programming paradigms'

• Programming Concepts An Overview Eager and Lazy Evaluation Procedures Functions External and Internal State Concurrency Static vs Dynamic Typing

• Some Real-Life Problems Search (Java vs Prolog) Higher Order Functions (Java vs Haskell)

1313Creative Commons Sharealike Attributions Noncommercial

Appreciating 'Programming paradigms'

• An example of how a trivial concept can affect/support a computation and hence a particular style of programming...

Int X;

Int Y = X;

Print( Y );

Execution Flow

?

1414Creative Commons Sharealike Attributions Noncommercial

Flow of the session

• Programming Paradigm - Definition

• Why bother about 'Programming Paradigms'?

• Appreciating 'Programming paradigms'

• Programming Concepts An Overview Eager and Lazy Evaluation Procedures Functions External and Internal State Concurrency Static vs Dynamic Typing

• Some Real-Life Problems Search (Java vs Prolog) Higher Order Functions (Java vs Haskell)

1515Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – An Overview

Set of Programming

Concepts

ProceduresFunctions

Higher/1st OrderEvaluation

Eager/LazyVariables

Single/Multi storeScope

Scope of variablesTypingState

Internal/ExternalObjectsClassesConcurrency& on & on & on.....

1616Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – An Overview

Subset ofConcepts

ProgrammingModel

Set ofProgramming

Concepts

ProgrammingLanguage(s)

Embracedby

Followedby

Embracedby

Followedby

1717Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – An Overview

Subset ofConcepts

OOProgramming

Model

Set ofProgramming

Concepts

OOProgrammingLanguage(s)

Embracedby

Followedby

• Java• Ruby• Oz• C++• Scala• Etc...

• Objects• Classes• Inheritance• Polymorphism• Explicit State• Etc...

Embracedby

Followedby

1818Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – An Overview

Subset ofConcepts

FunctionalProgramming

Model

Set ofProgramming

Concepts

FunctionalProgrammingLanguage(s)

Embracedby

Followedby

• Lazy/Eager Evaluation• Functions• Higher Order Func.• Etc...

• Haskell• Erlang• ML• Scheme• Etc...

Embracedby

Followedby

1919Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – An Overview

Concepts

Java

Prolog, Mercury

Programmingmodel/paradigm

ProgrammingLanguages

Higher-Order Functions,Intrnal State, Determinism

Function OrientedProgramming model

Haskell, OCamel,ML, Scheme

Procedures, External State,Concurrency

Object OrientedProgramming model

Procedures, External State,Pointcuts, Advice, Weaving

Aspect OrientedProgramming model

AspectJ, AspectC#,AspectC++

Facts, Rules,Search, Determinism

Logic OrientedProgramming model

2020Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – An Overview

Functional + Object Oriented Scala

Oz

Programmingmodels

Multiple paradigmLanguages

Procedural+ Functional

+Object Oriented

2121Creative Commons Sharealike Attributions Noncommercial

• Few important concepts Eager Evaluation Lazy Evaluation Procedures Functions External State Internal State Concurrency Static vs Dynamic Typing

Programming Concepts – An Overview

2222Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Eager Evaluation

• An expression which is evaluated as soon as it gets bound to a variable

• Importance Simple Commonly found Easy implementation for languages

• Problems Expressions are always evaluated

• Languages supporting easy evaluation Almost all including java, c#, c, etc.

2323Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Eager Evaluation

....List Gen( int N ) { ArrayList list = new ArrayList(); list.add( N ); list.addAll( Gen(N+1) ); return list;}

void myMethod () { List K = Gen(1); System.out.println( "Elements: " + K.getElementAt( 3 ) );}....

2424Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Lazy Evaluation

• An expression is evaluated only when needed

• Also known as delayed evaluation or call-by-need.

• Importance To deal with potentially infinite data. Generally, calculations are not repeated.

• Problems Difficult at glance Difficult to predict time/space complexity

• Languages supporting Lazy Evaluation Haskell, Oz, Scheme, OCamel

2525Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Lazy Evaluation

declare fun lazy {Gen N} N | {Gen N+1}end

declare K = {Gen 1}

{Browse “Elements: ” + Nth K 3}

2626Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Procedures

• A code construct which can Execute a set of statements in an order Accept zero or more arguments Return zero or more results Produce a side effect

• Importance One of the most basic building block of any programming language A means of telling the system how to execute some behavior May be used as to abstract out functions, objects & classes

• Languages that support procedures C, C++, C#, Oz

proc {Max X Y ?Z} if X>=Y then Z=X else Z=Y endend

2727Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Procedures

local Default MaxOrDefault in

Default=10

proc {MaxOrDefault Arg ?Result}if Arg >= Default then

Result = Arg else

Result = Defaultend

end

local Result in{MaxOrDefault 5 Result}{Browse Result}

end

end

2828Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Functions

• A code construct which can Execute a set of statements in a given order Accept zero or more arguments Returns only one result and no side effect Pure functions avoid state and mutable data

• Importance A basic building block of many programming language A means of telling the system how to execute some behavior Can replicate a pure mathematical model

• Languages that support Functions Haskell, OCamel, ML, Oz

fun {Max X Y} if X>=Y then Z=X else Z=Y endend

Z = {Max 10, 20}

2929Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Functions (Higher Order)

• Higher Order Functions Accepts functions as arguments along with other types Can return a function as result Ordering means...

• 1st order :- having no functions as args

• 2nd order :- can have 1st order functions as args

• nth order :- can have n-1th order functions as args We can build abstraction using Higher Order Functions

map f [] = []map f (x:xs) = f x : map f xs

numbers = [7,9,13]inc x = x + 1more_numbers = map inc numbers

3030Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Functions (Higher Order)

listsum [] = 0listsum (x:xs) = x + listsum xs

listprod [] = 1listprod (x:xs) = x * listprod xs

fold op init [] = initfold op init (x:xs) = x `op` fold op init xs

listsum = fold (+) 0listprod = fold (*) 1

3131Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – External and Internal State

• Explicit State State remembrance across calls Importance

• One of the base concept for Object Orientation

• Implicit state Defined by the function for its internal calculations. Importance

• Storing temporary results which otherwise might require repetitive calls.

• Languages that support external state Object Oriented languages like Java, Smalltalk, etc Languages that have global variables

• Languages that support internal state All languages that have variables in procedures / functions

3232Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – External and Internal State

fun {Sum Numbers} Result = {NewCell 0} Input = {NewCell Numbers} proc {Sum} case @Input of nil then skip [] X | Y then Result := @Result + X Input := Y {Sum} end end in {Sum} @Resultend

3333Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Concurrency

• Multiple and independent executions

• From real-life problems point of view, the definition adds this clause “and communicates only when needed”

• Importance relates more to real-life problems response time improved due to parallel executions

• Problems race condition

• Languages that support concurrent execution Java, Oz

threadConcurrentFlow1 = {GenerateNumbers 1 10}

end

3434Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Concurrency

fun {GenerateNumbers FromArg ToArg}{Delay 100}if FromArg > ToArg then

nilelse

FromArg | {GenerateNumbers FromArg+1 ToArg}end

end

thread ConcurrentFlow1 = {GenerateNumbers 1 10}

end

threadConcurrentFlow2 = {GenerateNumbers 11 20}

end

{Browse ConcurrentFlow1}{Browse ConcurrentFlow2}

3535Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Concurrency

....public void main(String[] args) throws InterruptedException {

Temp temp = new Temp();

createAConcurrentFlow(1, 10);createAConcurrentFlow(11, 20);

}....private void createAConcurrentFlow(int FromArg, int ToArg) {

new Runnable() {public void run() {

try {new Temp().GenerateNumbers(FromArg, ToArg);

}catch (InterruptedException e) { }

}};

}....

3636Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Static Typing

• Type checking is performed during compile-time

• Variables should be defined before they're used.

• Variables can be cast into other types

• Variables don't get converted

• Importance Improved error-catching ability, efficiency, security partial program verification

• Problems reducing expressiveness of the programming language restrictions for the programmer on the programs he can write

• Languages that use static typing C, C++, C#, Java, ML, Pascal, Haskell

3737Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Static Typing

int num, sum; // explicit declarationnum = 5; // now use the variablessum = 10;sum = sum + num;

3838Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Dynamic Typing

• Type checking is performed during runtime

• It's not necessary to define the variable before used

• Variables can be converted to other types implicitly

• Importance variables can be bound to any first-class-citizen in the lang. (proc,

func, mix of data-types, etc.) independent compilations of the program (since no type checking

at compile time)

• Problems more type-error-prone code at run-time.

• Languages that uses dynamic typing JavaScript, Lisp, Perl, PHP, Prolog, Python, Ruby

3939Creative Commons Sharealike Attributions Noncommercial

Programming Concepts – Dynamic Typing

foo() {x = 1;x = 'hello';

}

4040Creative Commons Sharealike Attributions Noncommercial

Flow of the session

• Programming Paradigm Definition

• Why bother about 'Programming Paradigms'?

• Appreciating 'Programming paradigms'

• Programming Concepts An Overview Eager and Lazy Evaluation Procedures Functions External and Internal State Concurrency Static vs Dynamic Typing

• Some Real-Life Problems Search (Java vs Prolog) Higher Order Functions (Java vs Haskell)

4141Creative Commons Sharealike Attributions Noncommercial

Some Real-Life Problems – Prolog vs Java

%% Facts% male(hrithik).male(shahrukh).male(salman).male(abhishek).male(akshay).male(aamir).

female(diya).female(aishwarya).female(katrina).female(malaika).

parent(hrithik,shahrukh).parent(hrithik,salman).parent(hrithik,diya).parent(shahrukh,abhishek).parent(shahrukh,akshay).parent(salman,aishwarya).parent(salman,katrina).parent(salman,aamir).parent(diya,malaika).

%% Rules% father(X,Y) :- parent(X,Y), male(X).mother(X,Y) :- parent(X,Y), female(X).grandparent(X,Y) :- parent(X,Z), parent(Z,Y).paternalgrandfather(X,Y) :- father(X,Z), father(Z,Y).sibling(X,Y) :- parent(Z,X), parent(Z,Y).brothers(X,Y) :- sibling(X,Y),male(X),male(Y), \+ (X=Y).

%% Queries%

cmd: mother(diya,malaika)% Outupt: yes/no

cmd: mother(Mother,Child).% Output: lists (mother,child) pair found in facts % according to associations defined

4242Creative Commons Sharealike Attributions Noncommercial

Some Real-Life Problems – Statistics

LOC

Prolog 15 min little hard 25 LinesJava easy 7x times

Haskell 15 min hard 9 linesJava 45 min easy 90 lines

Ease ofImplementation

Ease of Understanding

Search (Java vs Prolog)

45 min, 1/3rd completeHiger Order Functions (Java vs Haskell)

4343Creative Commons Sharealike Attributions Noncommercial

How to approach Programming Paradigm?

• Our Way was... Concepts first, Languages Later.

• Other effective ways... Mastering any one programming model (e.g. Object Oriented)

and then comparing it with others (e.g. functional, procedural, etc.)

4444Creative Commons Sharealike Attributions Noncommercial

References

• Books Concepts, Techniques & Models Of Computer Programming

by Peter Van Roy & Seif Haridi

• Links On Web http://wikipedia.org http://www.infocheese.com/programmingparadigms.html

4545Creative Commons Sharealike Attributions Noncommercial

What we expect post this session

• Experimentation with other programming languages Functional language -> Haskell / ML / Scheme Object Oriented language -> Java, C++, C#, Scala Aspect Oriented language -> AspectJ, AspectC++ Logic language -> Prolog / Mercury Mixed languages -> Scala, Oz

• Sharing your experiences with us... A testimonial

“I want to thank you and DIRECTI for conducting such a marvelous session and I am very much indebted for that.

I fell in love with LISP programming language and found that it was very much ideal for signal processing algos.

The approach u have suggested was just great and the book you have suggested was really really great.” - Chinni Krishna, Mukt '08 Session

Attendee

Intelligent People. Uncommon Ideas.

4646

Questions???

janeve.g@directi.com & nilesh.m@directi.com

http://directi.comhttp://careers.directi.com

Download slides: http://wiki.directi.com

Intelligent People. Uncommon Ideas.

4747

Retrospective!!!

janeve.g@directi.com & nilesh.m@directi.com

http://directi.comhttp://careers.directi.com

Download slides: http://wiki.directi.com