Share
Embed
Take a Closer Look at the Programming Paradigms
Where does the programming paradigm come from? What are the major programming paradigms that I should know? Why should I know multiple programming paradigms?
Wisnu Adi Nurcahyo
November 05, 2022
Transcript
108 slides
-
Take a Closer Look at the Programming Paradigms Proclub Telkom
University November 5th, 2022 -
I currently work as a Software Engineer in a tech
company with OTA as its core business Backend! -
I currently work as a Software Engineer in a tech
company with OTA as its core business btw I draw this with a mouse :< Backend! -
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~ randomness activeness wisn98 wisn98 wisn wisn#6051 sometimes I shitpost a lot -
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? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! C++ assignment grammar actually pretty complex so I will oversimplify it. -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! C++ assignment grammar actually pretty complex so I will oversimplify it. assignment := data-type whitespace name whitespace ‘=’ whitespace expression whitespace ‘;’ -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! C++ assignment grammar actually pretty complex so I will oversimplify it. assignment := data-type whitespace name whitespace ‘=’ whitespace expression whitespace ‘;’ -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! C++ assignment grammar actually pretty complex so I will oversimplify it. assignment := data-type whitespace name whitespace ‘=’ whitespace expression whitespace ‘;’ -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! C++ assignment grammar actually pretty complex so I will oversimplify it. assignment := data-type whitespace name whitespace ‘=’ whitespace expression whitespace ‘;’ -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! C++ assignment grammar actually pretty complex so I will oversimplify it. assignment := data-type whitespace name whitespace ‘=’ whitespace expression whitespace ‘;’ -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax Is this valid? Check the grammar rules! C++ assignment grammar actually pretty complex so I will oversimplify it. assignment := data-type whitespace name whitespace ‘=’ whitespace expression whitespace ‘;’ valid! -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } semantic -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } semantic What is the meaning of this? How do we evaluate this? What is the order of the execution? -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } semantic What is the meaning of this? How do we evaluate this? What is the order of the execution? execution model -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } semantic What is the meaning of this? How do we evaluate this? What is the order of the execution? execution model How does the whole program executed? -
Now, what is a programming language? int main() { int
a = 1; int b = 2; printf("%d\n", a + b); return 0; } semantic What is the meaning of this? How do we evaluate this? What is the order of the execution? execution model 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: execution model model of computation Focus on the order of the execution. Like, which one should be executed first before the other? Focus on the evaluation of a 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: execution model model of computation Focus on the order of the execution. Like, which one should be executed first before the other? Focus on the evaluation of a 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. Let’s just ignore this for now because this is happening in the low level. This is the responsibility of the compiler. -
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: execution model model of computation Focus on the order of the execution. Like, which one should be executed first before the other? Focus on the evaluation of a 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. 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 • Pushdown automata • Register machines • Turing machines • and so on… • Lambda calculus • Combinatory logic • General recursive functions • and so on… -
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… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • and so on… -
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… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • and so on… -
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… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • 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 • Pushdown automata • Register machines • Turing machines • and so on… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • 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 • Pushdown automata • Register machines • Turing machines • and so on… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • and so on… Imperative Programming • Procedural programming • Object-oriented programming • and so on (if any) Declarative Programming • Functional programming • Logic programming • Constraint programming • Query language • and so on -
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… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • and so on… Imperative Programming • Procedural programming • Object-oriented programming • and so on (if any) Declarative Programming • Functional programming • Logic programming • Constraint programming • Query language • and so on -
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… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • and so on… Imperative Programming • Procedural programming • Object-oriented programming • and so on (if any) Declarative Programming • Functional programming • Logic programming • Constraint programming • Query language • and so on Concurrent Programming • Actor-based programming • Choreographic programming • and so on -
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… • Lambda calculus • Combinatory logic • General recursive functions • and so on… • Actor model • Cellular automaton • Interaction nets • and so on… Imperative Programming • Procedural programming • Object-oriented programming • and so on (if any) Declarative Programming • Functional programming • Logic programming • Constraint programming • Query language • and so on Concurrent Programming • Actor-based programming • Choreographic programming • and so on -
What is that? Imperative programming? It uses statements that change
a program's state. -
What is that? Imperative programming? 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. -
What is that? Imperative programming? 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 } ... -
What is that? Imperative programming? 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 } ... flow -
What is that? Imperative programming? 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 } ... flow control flow -
What is that? Imperative programming? 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 } ... flow control flow state -
What is that? Imperative programming? 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 } ... flow control flow state i += 1 change the state -
What is that? Imperative programming? 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? -
What is that? Imperative programming? 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? ... 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; } } ... Imperative -
How do we see imperative, procedural, and object-oriented? ... 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; } } ... Imperative ... int n, target; vector<int> arr; get_input(n, target, arr); solve(arr, 0, n - 1, target); ... void solve(vector<int>& arr, int left, int right, int target) { if (arr[left] + arr[right] == target) { cout << left << “ “ << right << endl; return; } if (arr[left] + arr[right] > target) { solve(arr, left, right - 1, target); } else { solve(arr, left + 1, right, target); } } Procedural -
How do we see imperative, procedural, and object-oriented? ... 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; } } ... Imperative ... int n, target; vector<int> arr; get_input(n, target, arr); solve(arr, 0, n - 1, target); ... void solve(vector<int>& arr, int left, int right, int target) { if (arr[left] + arr[right] == target) { cout << left << “ “ << right << endl; return; } if (arr[left] + arr[right] > target) { solve(arr, left, right - 1, target); } else { solve(arr, left + 1, right, target); } } Procedural -
How do we see imperative, procedural, and object-oriented? ... 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; } } ... Imperative ... int n, target; vector<int> arr; get_input(n, target, arr); solve(arr, 0, n - 1, target); ... void solve(vector<int>& arr, int left, int right, int target) { if (arr[left] + arr[right] == target) { cout << left << “ “ << right << endl; return; } if (arr[left] + arr[right] > target) { solve(arr, left, right - 1, target); } else { solve(arr, left + 1, right, target); } } Procedural -
How do we see imperative, procedural, and object-oriented? ... 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; } } ... Imperative ... int n, target; vector<int> arr; get_input(n, target, arr); solve(arr, 0, n - 1, target); ... void solve(vector<int>& arr, int left, int right, int target) { if (arr[left] + arr[right] == target) { cout << left << “ “ << right << endl; return; } if (arr[left] + arr[right] > target) { solve(arr, left, right - 1, target); } else { solve(arr, left + 1, right, target); } } Procedural -
How do we see imperative, procedural, and object-oriented? ... 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; } } ... ... Solver* solver = new Solver(); solver->get_input(); solver->solve(); ... class Solver { ... void solve() { this->run_solver(0, this->len - 1); } void run_solver(int left, int right) { if (this->arr[left] + this->arr[right] == this->target) { cout << left << “ “ << right << endl; return; } if (this->arr[left] + this->arr[right] > this->target) { this->run_solver(left, right - 1); } else { this->run_solver(left + 1, right); } } ... } Imperative Object-oriented -
How do we see imperative, procedural, and object-oriented? ... 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; } } ... ... Solver* solver = new Solver(); solver->get_input(); solver->solve(); ... class Solver { ... void solve() { this->run_solver(0, this->len - 1); } void run_solver(int left, int right) { if (this->arr[left] + this->arr[right] == this->target) { cout << left << “ “ << right << endl; return; } if (this->arr[left] + this->arr[right] > this->target) { this->run_solver(left, right - 1); } else { this->run_solver(left + 1, right); } } ... } Imperative Object-oriented -
How do we see imperative, procedural, and object-oriented? ... 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; } } ... ... Solver* solver = new Solver(); solver->get_input(); solver->solve(); ... class Solver { ... void solve() { this->run_solver(0, this->len - 1); } void run_solver(int left, int right) { if ( this->arr[left] + this->arr[right] == this->target) { cout << left << “ “ << right << endl; return; } if (this->arr[left] + this->arr[right] > this->target) { this->run_solver(left, right - 1); } else { this->run_solver(left + 1, right); } } ... } Imperative Object-oriented retrieve data from the encapsulated properties -
How do we see imperative, procedural, and object-oriented? ... Solver*
solver = new Solver(); solver->get_input(); solver->solve(); ... class Solver { ... void solve() { this->run_solver(0, this->len - 1); } void run_solver(int left, int right) { if (this->arr[left] + this->arr[right] == this->target) { cout << left << “ “ << right << endl; return; } if (this->arr[left] + this->arr[right] > this->target) { this->run_solver(left, right - 1); } else { this->run_solver(left + 1, right); } } ... } Object-oriented ... int n, target; vector<int> arr; get_input(n, target, arr); solve(arr, 0, n - 1, target); ... void solve(vector<int>& arr, int left, int right, int target) { if (arr[left] + arr[right] == target) { cout << left << “ “ << right << endl; return; } if (arr[left] + arr[right] > target) { solve(arr, left, right - 1, target); } else { solve(arr, left + 1, right, target); } } Procedural -
How do we see imperative, procedural, and object-oriented? ... Solver*
solver = new Solver(); solver->get_input(); solver->solve(); ... class Solver { ... void solve() { this->run_solver(0, this->len - 1); } void run_solver(int left, int right) { if (this->arr[left] + this->arr[right] == this->target) { cout << left << “ “ << right << endl; return; } if (this->arr[left] + this->arr[right] > this->target) { this->run_solver(left, right - 1); } else { this->run_solver(left + 1, right); } } ... } Object-oriented ... int n, target; vector<int> arr; get_input(n, target, arr); solve(arr, 0, n - 1, target); ... void solve(vector<int>& arr, int left, int right, int target) { if (arr[left] + arr[right] == target) { cout << left << “ “ << right << endl; return; } if (arr[left] + arr[right] > target) { solve(arr, left, right - 1, target); } else { solve(arr, left + 1, right, target); } } Procedural flow -
How do we see imperative, procedural, and object-oriented? ... Solver*
solver = new Solver(); solver->get_input(); solver->solve(); ... class Solver { ... void solve() { this->run_solver(0, this->len - 1); } void run_solver(int left, int right) { if (this->arr[left] + this->arr[right] == this->target) { cout << left << “ “ << right << endl; return; } if (this->arr[left] + this->arr[right] > this->target) { this->run_solver(left, right - 1); } else { this->run_solver(left + 1, right); } } ... } Object-oriented ... int n, target; vector<int> arr; get_input(n, target, arr); solve(arr, 0, n - 1, target); ... void solve(vector<int>& arr, int left, int right, int target) { if (arr[left] + arr[right] == target) { cout << left << “ “ << right << endl; return; } if (arr[left] + arr[right] > target) { solve(arr, left, right - 1, target); } else { solve(arr, left + 1, right, target); } } Procedural flow both are imperative -
What is that? Declarative programming? It focuses on what to
execute, defines program logic, but not detailed control flow. -
What is that? Declarative programming? 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; -
What is that? Declarative programming? 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? -
What is that? Declarative programming? 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! -
What is that? Declarative programming? 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? -
What is that? Declarative programming? 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! -
What is that? Declarative programming? 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. -
What is that? Declarative programming? 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? -
What is that? Declarative programming? 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! -
What is that? Declarative programming? Now, both functional programming and
logic programming are classified as declarative programming. What’s the difference though? -
What is that? Declarative programming? 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) ... -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? Depends on the order of the function! -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? Depends on the order of the function! f(g(h(x))) Which one is called first? -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? Depends on the order of the function! f(g(h(x))) Which one is called first? Of course, it is h(x)! -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? Depends on the order of the function! f(g(h(x))) Which one is called first? Of course, it is h(x)! 1 -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? Depends on the order of the function! f(g(h(x))) Which one is called first? Of course, it is h(x)! 2 -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? Depends on the order of the function! f(g(h(x))) Which one is called first? Of course, it is h(x)! 3 called -
What is that? Declarative programming? 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) ... Basically, everything is a function. We compose many functions to get a result. How about the execution order? Depends on the order of the function! f(g(h(x))) Which one is called first? Of course, it is h(x)! and so on -
What is that? Declarative programming? 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). -
What is that? Declarative programming? 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). In logic programming, we evaluate things based on the facts. Is it true or is it false? The execution will stop whenever it encounter false fact. If we use AND condition, it will evaluate facts sequentially from left to the right. The left one must be true so the right one can be evaluated. If we use OR condition, it will evaluate everything without much care whether the left one is true or false. -
What is that? Declarative programming? 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). In logic programming, we evaluate things based on the facts. Is it true or is it false? The execution will stop whenever it encounter false fact. If we use AND condition, it will evaluate facts sequentially from left to the right. The left one must be true so the right one can be evaluated. If we use OR condition, it will evaluate everything without much care whether the left one is true or false. Which one is true? -
What is that? Declarative programming? 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). In logic programming, we evaluate things based on the facts. Is it true or is it false? The execution will stop whenever it encounter false fact. If we use AND condition, it will evaluate facts sequentially from left to the right. The left one must be true so the right one can be evaluated. If we use OR condition, it will evaluate everything without much care whether the left one is true or false. The result will be true as long as X less than or equal H -
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!