Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Take a Closer Look at the Programming Paradigms

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?

Avatar for Wisnu Adi Nurcahyo

Wisnu Adi Nurcahyo

November 05, 2022
Tweet

More Decks by Wisnu Adi Nurcahyo

Other Decks in Programming

Transcript

  1. I currently work as a Software Engineer in a tech

    company with OTA as its core business Backend!
  2. 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!
  3. 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
  4. 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
  5. Some might answer why not? Well, I believe we program

    a computer because we need it to help us to solve a (computational) problem.
  6. 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.
  7. 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?
  8. 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
  9. Now, what is a programming language? int main() { int

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

    a = 1; int b = 2; printf("%d\n", a + b); return 0; } syntax
  11. 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?
  12. 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?
  13. 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!
  14. 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.
  15. 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 ‘;’
  16. 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 ‘;’
  17. 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 ‘;’
  18. 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 ‘;’
  19. 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 ‘;’
  20. 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!
  21. Now, what is a programming language? int main() { int

    a = 1; int b = 2; printf("%d\n", a + b); return 0; } semantic
  22. 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?
  23. 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
  24. 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?
  25. 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
  26. 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.
  27. 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.
  28. 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.
  29. 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.
  30. 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…
  31. 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…
  32. 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…
  33. 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…
  34. 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)
  35. 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)
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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.
  41. 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 } ...
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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?
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. What is that? Declarative programming? It focuses on what to

    execute, defines program logic, but not detailed control flow.
  60. 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;
  61. 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?
  62. 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!
  63. 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?
  64. 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!
  65. 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.
  66. 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?
  67. 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!
  68. What is that? Declarative programming? Now, both functional programming and

    logic programming are classified as declarative programming. What’s the difference though?
  69. 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) ...
  70. 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.
  71. 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?
  72. 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!
  73. 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?
  74. 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)!
  75. 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
  76. 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
  77. 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
  78. 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
  79. 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).
  80. 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.
  81. 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?
  82. 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
  83. 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.
  84. 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.
  85. 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!