TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
TYPES OF PROGRAMMING LANGUAGES Scheme Declarative Functional Object-oriented Imperative Logic Concatenative Factor Forth Postscript Hilog Prolog Haskell Scala C ML Lisp Swift Objective C Java Ruby Python C++ Fortran Ada Modula Pascal 5/30
THE LANGUAGES I USE • When I need to write a Haskell • When I’d rather teach you Scheme • Never Java • When I’m told to use Java Scala prototype quickly Python Rust or C++ • When I want to solve puzzles Prolog write elegant code that I trust • When I want to have fun and Haskell • When I feel nostalgic C • When I need performance 6/30
• Focus on telling the • Focus on telling the computer what IMPERATIVE VS DECLARATIVE PROGRAMMING optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: Imperative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30
• Focus on telling the computer what IMPERATIVE VS DECLARATIVE PROGRAMMING optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: Imperative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
IMPERATIVE VS DECLARATIVE PROGRAMMING Imperative programming: optimize/parallelize • Easier to analyze/automatically penalties • Can come with performance in run-time system • Need to include evaluation engine • More high-level, elegant, expressive to do, not how to do it Declarative programming: • Side effects • Return values • Functions called for automatically optimize • Difficult to analyze/ • Close to the machine steps to execute computer exactly which 7/30 • Focus on telling the • Focus on telling the computer what
SIDE EFFECTS • Variable updates, I/O (disk, screen, keyboard, network, …) … are evil: • Source of 90% of all software bugs … are the only reason we compute at all: • Taking input and communicating results requires side effects. 8/30
SIDE EFFECTS • Variable updates, I/O (disk, screen, keyboard, network, …) … are evil: • Source of 90% of all software bugs … are the only reason we compute at all: • Taking input and communicating results requires side effects. 8/30
SIDE EFFECTS • Variable updates, I/O (disk, screen, keyboard, network, …) … are evil: • Source of 90% of all software bugs … are the only reason we compute at all: • Taking input and communicating results requires side effects. 8/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than easy to parallelize • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined 9/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than easy to parallelize • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined 9/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than easy to parallelize • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined 9/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than easy to parallelize • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined 9/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than easy to parallelize • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined 9/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than easy to parallelize • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined 9/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than easy to parallelize • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined 9/30
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than 9/30 • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined → easy to parallelize
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than 9/30 • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined → easy to parallelize
FUNCTIONAL PROGRAMMING Cons: functional counterparts efficient than their purely structures are inherently more • Some imperative data well-designed imperative code • Can be less efficient than 9/30 • Functions have no side effects • No specified execution order • Easier to analyze/optimize Pros: • Can express imperative computations elegantly! • Functions are first-class objects • Variables are immutable once defined → easy to parallelize
LOGIC PROGRAMMING • In theory, no need to worry • Obtain efficient programs • Avoid infinite loops in deduction execution details enough to • In practice, need to understand Cons: about execution details at all functional programming General program structure: • Even higher abstraction than Pros: • Engine to perform the deduction process efficiently Runtime system: • Execution driven by queries whether certain facts are true • Rules for deducing new facts from known facts • Database of facts, represented as logical predicates 10/30
LOGIC PROGRAMMING • In theory, no need to worry • Obtain efficient programs • Avoid infinite loops in deduction execution details enough to • In practice, need to understand Cons: about execution details at all functional programming General program structure: • Even higher abstraction than Pros: • Engine to perform the deduction process efficiently Runtime system: • Execution driven by queries whether certain facts are true • Rules for deducing new facts from known facts • Database of facts, represented as logical predicates 10/30
LOGIC PROGRAMMING • In theory, no need to worry • Obtain efficient programs • Avoid infinite loops in deduction execution details enough to • In practice, need to understand Cons: about execution details at all functional programming General program structure: • Even higher abstraction than Pros: • Engine to perform the deduction process efficiently Runtime system: • Execution driven by queries whether certain facts are true • Rules for deducing new facts from known facts • Database of facts, represented as logical predicates 10/30
LOGIC PROGRAMMING • In theory, no need to worry • Obtain efficient programs • Avoid infinite loops in deduction execution details enough to • In practice, need to understand Cons: about execution details at all functional programming General program structure: • Even higher abstraction than Pros: • Engine to perform the deduction process efficiently Runtime system: • Execution driven by queries whether certain facts are true • Rules for deducing new facts from known facts • Database of facts, represented as logical predicates 10/30
LOGIC PROGRAMMING • In theory, no need to worry • Obtain efficient programs • Avoid infinite loops in deduction execution details enough to • In practice, need to understand Cons: about execution details at all functional programming General program structure: • Even higher abstraction than Pros: • Engine to perform the deduction process efficiently Runtime system: • Execution driven by queries whether certain facts are true • Rules for deducing new facts from known facts • Database of facts, represented as logical predicates 10/30
LOGIC PROGRAMMING • In theory, no need to worry • Obtain efficient programs • Avoid infinite loops in deduction execution details enough to • In practice, need to understand Cons: about execution details at all functional programming General program structure: • Even higher abstraction than Pros: • Engine to perform the deduction process efficiently Runtime system: • Execution driven by queries whether certain facts are true • Rules for deducing new facts from known facts • Database of facts, represented as logical predicates 10/30
LOGIC PROGRAMMING • In theory, no need to worry • Obtain efficient programs • Avoid infinite loops in deduction execution details enough to • In practice, need to understand Cons: about execution details at all functional programming General program structure: • Even higher abstraction than Pros: • Engine to perform the deduction process efficiently Runtime system: • Execution driven by queries whether certain facts are true • Rules for deducing new facts from known facts • Database of facts, represented as logical predicates 10/30
INTERMISSION: SCHEME AND PROLOG TUTORIALS 11/30
AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (1) merge_sort(mid, end); } merge(left, right, begin); right(mid, end); std::vector<std::iterator_traits<It>::value_type> left(begin, mid); std::vector<std::iterator_traits<It>::value_type> merge_sort(begin, mid); C++: auto mid = begin + n / 2; return; if (n < 2) auto n = end - begin; void merge_sort(const It &begin, const It &end) { template <typename It> 12/30
AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (2) else } *out++ = *r++; while (r != right.end()) *out++ = *l++; while (l != left.end()) } *out++ = *l++; *out++ = *r++; template <typename It> if (*r < *l) while (l != left.end() && r != right.end()) { auto l = left.begin(), r = right.begin(); It out) { const std::vector<std::iterator_traits<It>::value_type> &right, const std::vector<std::iterator_traits<It>::value_type> &left, void merge( 13/30
AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (3) merge [] | otherwise = l : merge ls' rs rs' = r : merge ls merge ls@(l:ls') rs@(r:rs') | r < l = ls [] merge ls = rs rs merge :: Ord t => [t] -> [t] -> [t] Haskell: (ls, rs) = splitAt (n `div` 2) xs = length xs where n = merge (mergeSort ls) (mergeSort rs) mergeSort xs mergeSort [x] = [x] = [] mergeSort [] mergeSort :: Ord t => [t] -> [t] 14/30
AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (4) Prolog: list_sorted([], []). list_sorted([X], [X]). list_sorted(List, Sorted) :- list_left_right(List, Left, Right), list_sorted(Left, LeftSorted), list_sorted(Right, RightSorted), merged_left_right(Sorted, LeftSorted, RightSorted). merged_left_right(Left, Left, []). merged_left_right([R|Right], [], [R|Right]). merged_left_right([L|Merged], [L|Left], [R|Right]) :- L #=< R, merged_left_right(Merged, Left, [R|Right]). merged_left_right([R|Merged], [L|Left], [R|Right]) :- R #< L, merged_left_right(Merged, [L|Left], Right). 15/30
AN EXAMPLE WHERE FUNCTIONAL PROGRAMMING SHINES: MERGE SORT (5) list_left_right(List, Left, Right) :- phrase(parse_half(List, Left), List, Right). parse_half([], []) --> []. parse_half([_], []) --> []. parse_half([_,_|List], [L|Left]) --> [L], parse_half(List, Left). 16/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 3 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 4 1 5 6 0 4 2 6 1 3 5 0 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 3 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 4 1 5 6 0 4 2 6 1 3 5 0 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 3 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 4 1 5 6 0 4 2 6 1 3 5 0 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 3 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 4 1 5 6 0 4 2 6 1 3 5 0 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: 4 0 2 1 3 0 4 2 1 3 0 2 2 1 3 5 1 2 3 4 5 6 Input: Output: 1 0 DYNAMIC DATA STRUCTURES (1) 1 indicating, for each element, after which element it is to be inserted. Example: 0 0 1 0 3 2 1 2 3 4 5 6 0 4 2 6 1 3 5 0 17/30 Problem: Build a permutation of the integers { 0 , 1 , . . . , n − 1 } specified by
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: } 5 3 1 6 2 4 0 return std::vector<int>(seq.begin(), seq.end()); DYNAMIC DATA STRUCTURES (2) list_nodes[i] = seq.insert(next(list_nodes[ref[i]]), i); for (int i = 1; i < n; ++i) list_nodes[0] = seq.insert(seq.end(), 0); std::vector<std::list<int>::iterator> list_nodes(n); std::list<int> seq; int n = ref.size() + 1; std::vector<int> dynamic_permute(const std::vector<int> &refs) { C++: Linear time 18/30
This makes standard pointer-based data structures difficult/impossible to AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3) Doing this without mutation: 0 3 1 2 0 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. implement functionally. 19/30
This makes standard pointer-based data structures difficult/impossible to AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3) Doing this without mutation: 0 3 1 2 0 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. implement functionally. 19/30
This makes standard pointer-based data structures difficult/impossible to AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3) Doing this without mutation: 0 3 1 2 0 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. implement functionally. 19/30
This makes standard pointer-based data structures difficult/impossible to AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3) Doing this without mutation: 0 3 1 2 0 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. implement functionally. 19/30
This makes standard pointer-based data structures difficult/impossible to AN EXAMPLE WHERE IMPERATIVE PROGRAMMING SHINES: DYNAMIC DATA STRUCTURES (3) Doing this without mutation: 0 3 1 2 0 1 2 When “updating” any node in a functional data structure, all nodes with a path of pointers to it need to be replaced too. implement functionally. 19/30
Recommend
More recommend