http://cs.lmu.edu/~ray/html
A programming paradigm is a style or "way" of programming. Some languages make it easy to write in some paradigms but not others.sql
Some of the more common paradigms areexpress
Paradigms are not meant to be mutually exclusive; you can program in a functional, object-oriented, event-driven style. Make sure to check out Wikipedia's entry on Programming Paradigms.app
Control flow in imperative programming is explicit: commands show how the compuation takes place, step by step. Each step affects the global state of the computation.less
result =[]
i =0
start:
numPeople = length(people)
if i >= numPeople gotoend
p = people[i]
nameLength = length(p.name)
if nameLength <=5gotonext
upperName = toUpper(p.name)
addToList(result, upperName)
next:
i = i +1
goto start
end:
return sort(result)
Structured programming is a kind of imperative programming where the control flow is defined by nested loops, conditionals, and subroutines, rather than via gotos. Variables are generally local to blocks (have lexical scope).async
result =[];
for i =0; i < length(people); i++{
p = people[i];
if length(p.name))>5{
addToList(result, toUpper(p.name));
}
}
return sort(result);
Early languages emphasizing structured programming: Algol 60, PL/I, Algol 68, Pascal, C, Ada 83, Modula, Modula-2. Structured programming as a discipline is sometimes though to have been started by a famous letter by Edsger Dijkstra entitled Go to Statement Considered Harmful.ide
OOP is based on the sending of messages to objects. Objects respond to messages by performing operations. Messages can have arguments, so "sending messages" looks a lot like calling subroutines. A society of objects, each with their own "local memory" and own set of operations has a different feel than the "monolithic processor and single shared memory" feel of non object oriented languages.oop
result =[]
for p in people {
if p.name.length >5{
result.add(p.name.toUpper);
}
}
return result.sort;
The first object oriented language was Simula-67; Smalltalk followed soon after as the first "pure" object-oriented language. Many languages designed from the 1980s to the present have been object-oriented, notably C++, CLOS (object system of Common Lisp), Eiffel, Modula-3, Ada 95, Java, C#, Ruby.ui
Control flow in declarative programming is implicit: the programmer states only what the result should look like,not how to obtain it.this
select upper(name)
from people
where length(name)>5
orderby name
No loops, no assignments, etc. Whatever engine that interprets this code is just supposed go get the desired information, and can use whatever approach it wants. (The logic and relational paradigms are generally declarative as well.)
In functional programming control flow is expressed by combining function calls, rather than by assigning values to variables.
let(
f,fun(
people,
if(equals(people, emptylist),
emptylist,
if(greater(length(name(head(people))),5),
append(to_upper(name(head(people))), f(tail(people))),
f(tail(people))))),
sort(f(people)))
Of course, there's usually syntactic sugar
let
fun f []=[]
| f (p :: ps)=
if p.name.length()>5then p.name.to_upper()::(f ps)
else(f ps)
in
sort(f(people))
The real power of this paradigm comes from passing functions to functions (and returning functions from functions).
sort(
filter((λs. s.length()>5),
map((λp. p.name.to_upper()), people)
Read Joel Spolsky's article on map and reduce.
With functional programming
Some people like to say
Python has a neat little thing called list comprehensions that combine map and filter.
sorted([p.name.upper()for p in people if len(p.name)>5])
Logic and constraint programming are two paradigms in which programs are built by setting up relations that specify facts and inference rules, and asking whether or not something is true (i.e. specifying a goal.) Unification and backtracking to find solutions (i.e. satisfy goals) takes place automatically.
Languages that emphasize this paradigm: Prolog, GHC, Parlog, Vulcan, Polka, Mercury, Fnil.