Where does the programming paradigm come from? What are the major programming paradigms that I should know? Why should I know multiple programming paradigms?
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
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.
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 ‘;’
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 ‘;’
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 ‘;’
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 ‘;’
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 ‘;’
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!
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?
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
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?
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
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.
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.
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.
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…
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…
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…
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)
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)
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
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
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
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
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 } ...
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
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
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
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
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?
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
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
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;
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?
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!
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?
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!
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.
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?
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!
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) ...
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.
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?
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!
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?
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)!
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
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
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
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
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.
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?
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
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.
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.
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!