[go: up one dir, main page]

0% found this document useful (0 votes)
26 views108 pages

Take A Closer Look at The Programming Paradigms

The document discusses programming paradigms and introduces some key concepts about programming languages. It begins by asking what programming is and why we program computers. It then discusses that programming languages allow us to tell computers how to solve computational problems. The document goes on to explain that programming languages have syntax, semantics, and an execution model which define how programs are written and executed. It asks questions about the meaning and order of operations in a sample C++ program.

Uploaded by

emailbarukuiniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views108 pages

Take A Closer Look at The Programming Paradigms

The document discusses programming paradigms and introduces some key concepts about programming languages. It begins by asking what programming is and why we program computers. It then discusses that programming languages allow us to tell computers how to solve computational problems. The document goes on to explain that programming languages have syntax, semantics, and an execution model which define how programs are written and executed. It asks questions about the meaning and order of operations in a sample C++ program.

Uploaded by

emailbarukuiniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 108

Take a Closer Look

at the Programming
Paradigms

Proclub Telkom University


November 5th, 2022
Hello!
Hello!
Before we get started,
let me introduce myself!
I am Wisnu.
I am Wisnu. Also known as wisn.
I currently work as a
Software Engineer
Backend!
in a tech company with OTA
as its core business
I currently work as a
Software Engineer
Backend!
in a tech company with OTA
as its core business

btw I draw this with a mouse :<


Anyway, based on my job description today, I
think I was closer to be a

Software/Data Engineer
because I will be dealing with
both software development and
data pipeline development
If you ever have something to ask me, feel free to do so!
You may also connect or follow me on social media~
activeness

wisn#6051
wisn98
wisn

sometimes I shitpost a lot

randomness

wisn98
Alright!
Now back to the topic!
So, yes, programming…
So, yes, programming…
but

what exactly is programming?


Why would we program a
computer?
Some might answer why not?
Well, I believe we program a computer because we need
it to help us to solve a (computational) problem.
Some might answer why not?
Well, I believe we program a computer because we need
it to help us to solve a (computational) problem.

Basically, we tell a computer what to do to solve the problem.


Some might answer why not?
Well, I believe we program a computer because we need
it to help us to solve a (computational) problem.

Basically, we tell a computer what to do to solve the problem.

How?
Some might answer why not?
Well, I believe we program a computer because we need
it to help us to solve a (computational) problem.

Basically, we tell a computer what to do to solve the problem.

How? with a programming language


Now, what is a programming language?
Now, what is a programming language?
Now, what is a programming language?

int main() {
int a = 1;
int b = 2;

printf("%d\n", a + b);

return 0;
}
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;

printf("%d\n", a + b);

return 0;
}
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid?
printf("%d\n", a + b);

return 0;
}
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid?
printf("%d\n", a + b);

return 0;
}
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);

return 0;
}
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);
C++ assignment grammar actually pretty
return 0;
complex so I will oversimplify it.
}
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);
C++ assignment grammar actually pretty
return 0;
complex so I will oversimplify it.
}
assignment := data-type whitespace
name whitespace
‘=’ whitespace
expression whitespace ‘;’
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);
C++ assignment grammar actually pretty
return 0;
complex so I will oversimplify it.
}
assignment := data-type whitespace
name whitespace
‘=’ whitespace
expression whitespace ‘;’
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);
C++ assignment grammar actually pretty
return 0;
complex so I will oversimplify it.
}
assignment := data-type whitespace
name whitespace
‘=’ whitespace
expression whitespace ‘;’
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);
C++ assignment grammar actually pretty
return 0;
complex so I will oversimplify it.
}
assignment := data-type whitespace
name whitespace
‘=’ whitespace
expression whitespace ‘;’
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);
C++ assignment grammar actually pretty
return 0;
complex so I will oversimplify it.
}
assignment := data-type whitespace
name whitespace
‘=’ whitespace
expression whitespace ‘;’
Now, what is a programming language?
syntax
int main() {
int a = 1;
int b = 2;
Is this valid? Check the grammar rules!
printf("%d\n", a + b);
C++ assignment grammar actually pretty
return 0;
complex so I will oversimplify it.
}
assignment := data-type whitespace
name whitespace
‘=’ whitespace
expression whitespace ‘;’

valid!
Now, what is a programming language?

semantic
int main() {
int a = 1;
int b = 2;

printf("%d\n", a + b);

return 0;
}
Now, what is a programming language?

semantic
int main() {
int a = 1;
int b = 2;

printf("%d\n", a + b);
What is the meaning of this?
return 0; How do we evaluate this?
} What is the order of the execution?
Now, what is a programming language?

execution model semantic


int main() {
int a = 1;
int b = 2;

printf("%d\n", a + b);
What is the meaning of this?
return 0; How do we evaluate this?
} What is the order of the execution?
Now, what is a programming language?

execution model semantic


int main() {
int a = 1;
int b = 2;

printf("%d\n", a + b);
What is the meaning of this?
return 0; How do we evaluate this?
} What is the order of the execution?

How does the whole program executed?


Now, what is a programming language?

execution model semantic


int main() {
int a = 1;
int b = 2;

printf("%d\n", a + b);
What is the meaning of this?
return 0; How do we evaluate this?
} What is the order of the execution?

How does the whole program executed?


semantic creates a model of computation
What is the difference between an execution model and a model of computation?
What is the difference between an execution model and a model of computation?

To be honest, I’m not really sure. I can’t find a clear answer so far.
What is the difference between an execution model and a model of computation?

To be honest, I’m not really sure. I can’t find a clear answer so far.

However, I believe it is something like this:

Focus on the order of the execution.


execution model Like, which one should be executed first before the other?

Focus on the evaluation of a computation.


model of computation Like, when we give an expression, how is the evaluation
looks like so it produce a (correct) result?
How do we manage the memory and so on.
What is the difference between an execution model and a model of computation?

To be honest, I’m not really sure. I can’t find a clear answer so far.

Let’s just ignore this for now because this is


happening in the low level. This is the responsibility
However, I believe it is something like this: of the compiler.

Focus on the order of the execution.


execution model Like, which one should be executed first before the other?

Focus on the evaluation of a computation.


model of computation Like, when we give an expression, how is the evaluation
looks like so it produce a (correct) result?
How do we manage the memory and so on.
What is the difference between an execution model and a model of computation?

To be honest, I’m not really sure. I can’t find a clear answer so far.

However, I believe it is something like this:

Focus on the order of the execution.


execution model Like, which one should be executed first before the other?

Focus on the evaluation of a computation.


model of computation Like, when we give an expression, how is the evaluation
looks like so it produce a (correct) result?
How do we manage the memory and so on.

We focus on this instead. This is basically where we model our solution through programming.
Also, this is related to our main topic which is programming paradigm.
Maybe this is how we see it.

execution model

model of computation
Model of computations are classified into three categories.
Model of computations are classified into three categories.

Sequential Model
Model of computations are classified into three categories.

Sequential Model Functional Model


Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines
● Pushdown automata
● Register machines
● Turing machines
● and so on…
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus
● Pushdown automata ● Combinatory logic
● Register machines ● General recursive
● Turing machines functions
● and so on… ● and so on…
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…

Imperative Programming
● Procedural programming
● Object-oriented programming
● and so on (if any)
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…

Imperative Programming
● Procedural programming
● Object-oriented programming
● and so on (if any)
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…

Declarative Programming
Imperative Programming
● Functional programming
● Procedural programming
● Logic programming
● Object-oriented programming
● Constraint programming
● and so on (if any)
● Query language
● and so on
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…

Declarative Programming
Imperative Programming
● Functional programming
● Procedural programming
● Logic programming
● Object-oriented programming
● Constraint programming
● and so on (if any)
● Query language
● and so on
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…

Declarative Programming
Imperative Programming Concurrent Programming
● Functional programming
● Procedural programming ● Actor-based programming
● Logic programming
● Object-oriented programming ● Choreographic programming
● Constraint programming
● and so on (if any) ● and so on
● Query language
● and so on
Model of computations are classified into three categories.

Sequential Model Functional Model Concurrent Model


● Finite state machines ● Lambda calculus ● Actor model
● Pushdown automata ● Combinatory logic ● Cellular automaton
● Register machines ● General recursive ● Interaction nets
● Turing machines functions ● and so on…
● and so on… ● and so on…

Declarative Programming
Imperative Programming Concurrent Programming
● Functional programming
● Procedural programming ● Actor-based programming
● Logic programming
● Object-oriented programming ● Choreographic programming
● Constraint programming
● and so on (if any) ● and so on
● Query language
● and so on
Imperative programming? What is that?
Imperative programming? What is that?

It uses statements that change a program's state.


Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.
Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.

...

int i = 0;
int n = list.size();
while (i < n) {
// take something
// compute something
// put something
}

...
Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.

...

int i = 0;
int n = list.size();
while (i < n) {
// take something flow
// compute something
// put something
}

...
Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.

...

int i = 0;
int n = list.size();
while (i < n) {
// take something flow
// compute something
// put something
}

...

control flow
Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.

... state

int i = 0;
int n = list.size();
while (i < n) {
// take something flow
// compute something
// put something
}

...

control flow
Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.

... state

int i = 0;
int n = list.size();
while (i < n) {
// take something flow
// compute something
// put something
}
change the state
...

i += 1
control flow
Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.

Both procedural programming and object-oriented programming are derived from imperative programming.
What is the difference between the two?
Imperative programming? What is that?

It uses statements that change a program's state.


The focus is more on the how to execute, defines control flow as statements that change a program state.

Both procedural programming and object-oriented programming are derived from imperative programming.
What is the difference between the two?

Procedural Object-oriented

Procedure Method

Record Object

Module Class

Procedure call Message


How do we see imperative, procedural, and object-oriented?
How do we see imperative, procedural, and object-oriented?

Imperative
...

int n, target;
cin >> n >> target;

vector<int> arr (n);


for (int i = 0; i < n; i += 1) {
cin >> arr[i];
}

int left = 0, right = n - 1;


while (left < right) {
if (arr[left] + arr[right] == target) {
cout << left << “ “ << right << endl;
break;
}

if (arr[left] + arr[right] > target) {


right -= 1;
} else {
left += 1;
}
}

...
How do we see imperative, procedural, and object-oriented?

Imperative Procedural
... ...

int n, target; int n, target;


cin >> n >> target; vector<int> arr;

vector<int> arr (n); get_input(n, target, arr);


for (int i = 0; i < n; i += 1) {
cin >> arr[i]; solve(arr, 0, n - 1, target);
}
...
int left = 0, right = n - 1;
while (left < right) { void solve(vector<int>& arr, int left, int right, int target) {
if (arr[left] + arr[right] == target) { if (arr[left] + arr[right] == target) {
cout << left << “ “ << right << endl; cout << left << “ “ << right << endl;
break; return;
} }

if (arr[left] + arr[right] > target) { if (arr[left] + arr[right] > target) {


right -= 1; solve(arr, left, right - 1, target);
} else { } else {
left += 1; solve(arr, left + 1, right, target);
} }
} }

...
How do we see imperative, procedural, and object-oriented?

Imperative Procedural
... ...

int n, target; int n, target;


cin >> n >> target; vector<int> arr;

vector<int> arr (n); get_input(n, target, arr);


for (int i = 0; i < n; i += 1) {
cin >> arr[i]; solve(arr, 0, n - 1, target);
}
...
int left = 0, right = n - 1;
while (left < right) { void solve(vector<int>& arr, int left, int right, int target) {
if (arr[left] + arr[right] == target) { if (arr[left] + arr[right] == target) {
cout << left << “ “ << right << endl; cout << left << “ “ << right << endl;
break; return;
} }

if (arr[left] + arr[right] > target) { if (arr[left] + arr[right] > target) {


right -= 1; solve(arr, left, right - 1, target);
} else { } else {
left += 1; solve(arr, left + 1, right, target);
} }
} }

...
How do we see imperative, procedural, and object-oriented?

Imperative Procedural
... ...

int n, target; int n, target;


cin >> n >> target; vector<int> arr;

vector<int> arr (n); get_input(n, target, arr);


for (int i = 0; i < n; i += 1) {
cin >> arr[i]; solve(arr, 0, n - 1, target);
}
...
int left = 0, right = n - 1;
while (left < right) { void solve(vector<int>& arr, int left, int right, int target) {
if (arr[left] + arr[right] == target) { if (arr[left] + arr[right] == target) {
cout << left << “ “ << right << endl; cout << left << “ “ << right << endl;
break; return;
} }

if (arr[left] + arr[right] > target) { if (arr[left] + arr[right] > target) {


right -= 1; solve(arr, left, right - 1, target);
} else { } else {
left += 1; solve(arr, left + 1, right, target);
} }
} }

...
How do we see imperative, procedural, and object-oriented?

Imperative Procedural
... ...

int n, target; int n, target;


cin >> n >> target; vector<int> arr;

vector<int> arr (n); get_input(n, target, arr);


for (int i = 0; i < n; i += 1) {
cin >> arr[i]; solve(arr, 0, n - 1, target);
}
...
int left = 0, right = n - 1;
while (left < right) { void solve(vector<int>& arr, int left, int right, int target) {
if (arr[left] + arr[right] == target) { if (arr[left] + arr[right] == target) {
cout << left << “ “ << right << endl; cout << left << “ “ << right << endl;
break; return;
} }

if (arr[left] + arr[right] > target) { if (arr[left] + arr[right] > target) {


right -= 1; solve(arr, left, right - 1, target);
} else { } else {
left += 1; solve(arr, left + 1, right, target);
} }
} }

...
How do we see imperative, procedural, and object-oriented?

Imperative Object-oriented
... ...

int n, target; Solver* solver = new Solver();


solver->get_input();
cin >> n >> target;
solver->solve();

vector<int> arr (n); ...


for (int i = 0; i < n; i += 1) {
cin >> arr[i]; class Solver {
} ...
void solve() {
int left = 0, right = n - 1; this->run_solver(0, this->len - 1);
}
while (left < right) {
if (arr[left] + arr[right] == target) { void run_solver(int left, int right) {
cout << left << “ “ << right << endl; if (this->arr[left] + this->arr[right] == this->target) {
break; cout << left << “ “ << right << endl;
} return;
}
if (arr[left] + arr[right] > target) {
right -= 1; if (this->arr[left] + this->arr[right] > this->target) {
this->run_solver(left, right - 1);
} else {
} else {
left += 1; this->run_solver(left + 1, right);
} }
} }
...
... }
How do we see imperative, procedural, and object-oriented?

Imperative Object-oriented
... ...

int n, target; Solver* solver = new Solver();


solver->get_input();
cin >> n >> target;
solver->solve();

vector<int> arr (n); ...


for (int i = 0; i < n; i += 1) {
cin >> arr[i]; class Solver {
} ...
void solve() {
int left = 0, right = n - 1; this->run_solver(0, this->len - 1);
}
while (left < right) {
if (arr[left] + arr[right] == target) { void run_solver(int left, int right) {
cout << left << “ “ << right << endl; if (this->arr[left] + this->arr[right] == this->target) {
break; cout << left << “ “ << right << endl;
} return;
}
if (arr[left] + arr[right] > target) {
right -= 1; if (this->arr[left] + this->arr[right] > this->target) {
this->run_solver(left, right - 1);
} else {
} else {
left += 1; this->run_solver(left + 1, right);
} }
} }
...
... }
How do we see imperative, procedural, and object-oriented?

Imperative Object-oriented
... ...

int n, target; Solver* solver = new Solver();


solver->get_input();
cin >> n >> target;
solver->solve(); retrieve data from the
vector<int> arr (n); ... encapsulated properties
for (int i = 0; i < n; i += 1) {
cin >> arr[i]; class Solver {
} ...
void solve() {
int left = 0, right = n - 1; this->run_solver(0, this->len - 1);
}
while (left < right) {
if (arr[left] + arr[right] == target) { void run_solver(int left, int right) {
cout << left << “ “ << right << endl; if ( this->arr[left] + this->arr[right] == this->target) {
break; cout << left << “ “ << right << endl;
} return;
}
if (arr[left] + arr[right] > target) {
right -= 1; if (this->arr[left] + this->arr[right] > this->target) {
this->run_solver(left, right - 1);
} else {
} else {
left += 1; this->run_solver(left + 1, right);
} }
} }
...
... }
How do we see imperative, procedural, and object-oriented?

Procedural Object-oriented
... ...

int n, target; Solver* solver = new Solver();


solver->get_input();
vector<int> arr;
solver->solve();

get_input(n, target, arr); ...

solve(arr, 0, n - 1, target); class Solver {


...
... void solve() {
this->run_solver(0, this->len - 1);
}
void solve(vector<int>& arr, int left, int right, int target) {
if (arr[left] + arr[right] == target) { void run_solver(int left, int right) {
cout << left << “ “ << right << endl; if (this->arr[left] + this->arr[right] == this->target) {
return; cout << left << “ “ << right << endl;
} return;
}
if (arr[left] + arr[right] > target) {
solve(arr, left, right - 1, target); if (this->arr[left] + this->arr[right] > this->target) {
this->run_solver(left, right - 1);
} else {
} else {
solve(arr, left + 1, right, target); this->run_solver(left + 1, right);
} }
} }
...
}
How do we see imperative, procedural, and object-oriented?

Procedural Object-oriented
... ...

int n, target; Solver* solver = new Solver();


solver->get_input();
vector<int> arr;
solver->solve();

get_input(n, target, arr); ...

solve(arr, 0, n - 1, target); class Solver {


...
flow void solve() {
...
this->run_solver(0, this->len - 1);
}
void solve(vector<int>& arr, int left, int right, int target) {
if (arr[left] + arr[right] == target) { void run_solver(int left, int right) {
cout << left << “ “ << right << endl; if (this->arr[left] + this->arr[right] == this->target) {
return; cout << left << “ “ << right << endl;
} return;
}
if (arr[left] + arr[right] > target) {
solve(arr, left, right - 1, target); if (this->arr[left] + this->arr[right] > this->target) {
this->run_solver(left, right - 1);
} else {
} else {
solve(arr, left + 1, right, target); this->run_solver(left + 1, right);
} }
} }
...
}
How do we see imperative, procedural, and object-oriented?

Procedural Object-oriented
... ...

int n, target; Solver* solver = new Solver();


solver->get_input();
vector<int> arr;
solver->solve();

get_input(n, target, arr); ...

solve(arr, 0, n - 1, target); class Solver {


...
flow void solve() {
...
this->run_solver(0, this->len - 1);
}
void solve(vector<int>& arr, int left, int right, int target) {
if (arr[left] + arr[right] == target) { void run_solver(int left, int right) {
cout << left << “ “ << right << endl; if (this->arr[left] + this->arr[right] == this->target) {
return; cout << left << “ “ << right << endl;
} return;
}
if (arr[left] + arr[right] > target) {
solve(arr, left, right - 1, target); if (this->arr[left] + this->arr[right] > this->target) {
this->run_solver(left, right - 1);
} else {
} else {
solve(arr, left + 1, right, target); this->run_solver(left + 1, right);
} }
} }
...
}
both are imperative
Alright!
Next paradigms!
Declarative programming? What is that?
Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.
Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;
Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;

Do we know how this query executed under the hood?


Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;

Do we know how this query executed under the hood? NO!


Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;

Do we know how this query executed under the hood? NO!


Do we care?
Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;

Do we know how this query executed under the hood? NO!


Do we care? NO!
Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;

Do we know how this query executed under the hood? NO!


Do we care? NO!

However, what I know is that if I run this query then I will get a list of book (its id and name) with
“pro” as the starting name and the number of the book is 10.
Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;

Do we know how this query executed under the hood? NO!


Do we care? NO!

However, what I know is that if I run this query then I will get a list of book (its id and name) with
“pro” as the starting name and the number of the book is 10.

Is there any control flow or state changes?


Declarative programming? What is that?

It focuses on what to execute, defines program logic, but not detailed control flow.

SELECT book_id, book_name FROM books WHERE book_name ILIKE ‘pro%’ ORDER BY book_id ASC LIMIT
10;

Do we know how this query executed under the hood? NO!


Do we care? NO!

However, what I know is that if I run this query then I will get a list of book (its id and name) with
“pro” as the starting name and the number of the book is 10.

Is there any control flow or state changes? Well, no!


Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...

main :: IO ()
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input

gcd' :: Integral a => a -> a -> a


gcd' n 0 = n
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


listToTuple (x:xs:_) = (x,xs)

convertToInt :: [String] -> [Int]


convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...
Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input

gcd' :: Integral a => a -> a -> a


gcd' n 0 = n
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


listToTuple (x:xs:_) = (x,xs)

convertToInt :: [String] -> [Int]


convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...
Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


listToTuple (x:xs:_) = (x,xs)

convertToInt :: [String] -> [Int]


convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...
Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n Depends on the order of the function!
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


listToTuple (x:xs:_) = (x,xs)

convertToInt :: [String] -> [Int]


convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...
Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n Depends on the order of the function!
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


f(g(h(x)))
listToTuple (x:xs:_) = (x,xs)
Which one is called first?
convertToInt :: [String] -> [Int]
convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...
Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n Depends on the order of the function!
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


f(g(h(x)))
listToTuple (x:xs:_) = (x,xs)
Which one is called first? Of course, it is h(x)!
convertToInt :: [String] -> [Int]
convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
1
...
Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n Depends on the order of the function!
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


f(g(h(x)))
listToTuple (x:xs:_) = (x,xs)
Which one is called first? Of course, it is h(x)!
convertToInt :: [String] -> [Int]
convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
... 2 Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n Depends on the order of the function!
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


f(g(h(x)))
listToTuple (x:xs:_) = (x,xs)
Which one is called first? Of course, it is h(x)!
convertToInt :: [String] -> [Int]
convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...
3 Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n Depends on the order of the function!
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int) called f(g(h(x)))


listToTuple (x:xs:_) = (x,xs)
Which one is called first? Of course, it is h(x)!
convertToInt :: [String] -> [Int]
convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Functional
...
and so on Basically, everything is a function.
main :: IO () We compose many functions to get a result.
main = (print . uncurry gcd' . listToTuple . convertToInt . words) =<< input
How about the execution order?
gcd' :: Integral a => a -> a -> a
gcd' n 0 = n Depends on the order of the function!
gcd' n m = gcd' m (n `mod` m)

listToTuple :: [Int] -> (Int, Int)


f(g(h(x)))
listToTuple (x:xs:_) = (x,xs)
Which one is called first? Of course, it is h(x)!
convertToInt :: [String] -> [Int]
convertToInt = map (read :: String -> Int)

...
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

Logic
monotone([]) :- !.
monotone([_, _|[]]) :- !.
monotone([X, Y|T]) :- X =< Y, monotone_increasing([X, Y|T]), !.
monotone([X, Y|T]) :- monotone_decreasing([X, Y|T]).

monotone_increasing([]).
monotone_increasing([H|T]) :- monotone_increasing_helper(H, T), monotone_increasing(T).

monotone_increasing_helper(_, []).
monotone_increasing_helper(X, [H|T]) :- X =< H, monotone_increasing_helper(X, T).

monotone_decreasing([]) :- !.
monotone_decreasing([H|T]) :- monotone_decreasing_helper(H, T), monotone_decreasing(T).

monotone_decreasing_helper(_, []) :- !.
monotone_decreasing_helper(X, [H|T]) :- X >= H, monotone_decreasing_helper(X, T).
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

In logic programming, we evaluate


Logic things based on the facts.

monotone([]) :- !. Is it true or is it false?


monotone([_, _|[]]) :- !.
monotone([X, Y|T]) :- X =< Y, monotone_increasing([X, Y|T]), !. The execution will stop whenever it
monotone([X, Y|T]) :- monotone_decreasing([X, Y|T]). encounter false fact.

monotone_increasing([]). If we use AND condition, it will


monotone_increasing([H|T]) :- monotone_increasing_helper(H, T), monotone_increasing(T). evaluate facts sequentially from left
to the right. The left one must be
monotone_increasing_helper(_, []).
monotone_increasing_helper(X, [H|T]) :- X =< H, monotone_increasing_helper(X, T).
true so the right one can be
evaluated.
monotone_decreasing([]) :- !.
monotone_decreasing([H|T]) :- monotone_decreasing_helper(H, T), monotone_decreasing(T). If we use OR condition, it will
evaluate everything without much
monotone_decreasing_helper(_, []) :- !. care whether the left one is true or
monotone_decreasing_helper(X, [H|T]) :- X >= H, monotone_decreasing_helper(X, T). false.
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

In logic programming, we evaluate


Logic Which one is true?
things based on the facts.

monotone([]) :- !. Is it true or is it false?


monotone([_, _|[]]) :- !.
monotone([X, Y|T]) :- X =< Y, monotone_increasing([X, Y|T]), !. The execution will stop whenever it
monotone([X, Y|T]) :- monotone_decreasing([X, Y|T]). encounter false fact.

monotone_increasing([]). If we use AND condition, it will


monotone_increasing([H|T]) :- monotone_increasing_helper(H, T), monotone_increasing(T). evaluate facts sequentially from left
to the right. The left one must be
monotone_increasing_helper(_, []).
monotone_increasing_helper(X, [H|T]) :- X =< H, monotone_increasing_helper(X, T).
true so the right one can be
evaluated.
monotone_decreasing([]) :- !.
monotone_decreasing([H|T]) :- monotone_decreasing_helper(H, T), monotone_decreasing(T). If we use OR condition, it will
evaluate everything without much
monotone_decreasing_helper(_, []) :- !. care whether the left one is true or
monotone_decreasing_helper(X, [H|T]) :- X >= H, monotone_decreasing_helper(X, T). false.
Declarative programming? What is that?

Now, both functional programming and logic programming are classified as declarative programming.
What’s the difference though?

In logic programming, we evaluate


Logic The result will be true as long as X less than or equal H things based on the facts.

monotone([]) :- !. Is it true or is it false?


monotone([_, _|[]]) :- !.
monotone([X, Y|T]) :- X =< Y, monotone_increasing([X, Y|T]), !. The execution will stop whenever it
monotone([X, Y|T]) :- monotone_decreasing([X, Y|T]). encounter false fact.

monotone_increasing([]). If we use AND condition, it will


monotone_increasing([H|T]) :- monotone_increasing_helper(H, T), monotone_increasing(T). evaluate facts sequentially from left
to the right. The left one must be
monotone_increasing_helper(_, []).
monotone_increasing_helper(X, [H|T]) :- X =< H, monotone_increasing_helper(X, T).
true so the right one can be
evaluated.
monotone_decreasing([]) :- !.
monotone_decreasing([H|T]) :- monotone_decreasing_helper(H, T), monotone_decreasing(T). If we use OR condition, it will
evaluate everything without much
monotone_decreasing_helper(_, []) :- !. care whether the left one is true or
monotone_decreasing_helper(X, [H|T]) :- X >= H, monotone_decreasing_helper(X, T). false.
Why should we learn these different programming paradigms?
Why should we learn these different programming paradigms?

That is we know the characteristic and the strength of each programming paradigm.
Making it easy for us to choose the right toolbox to solve a certain problem.
After all, programming paradigm is something that we use to model a solution.
Why should we learn these different programming paradigms?

That is we know the characteristic and the strength of each programming paradigm.
Making it easy for us to choose the right toolbox to solve a certain problem.
After all, programming paradigm is something that we use to model a solution.

Like, some problems are easier to solve with step-by-step approach of the imperative programming.
However, some problems are actually easier to solve by composing many functions.
Or maybe, there are problems in which are easier to solve by combining facts.
Fin!
QnA session now, baby!
If there is something that need to be corrected, please feel free to correct me!
Do contact me and explain.
After all, just like you guys, I’m also still in the middle of learning things!

You might also like