types of programming languages
play

TYPES OF PROGRAMMING LANGUAGES PRINCIPLES OF PROGRAMMING LANGUAGES - PowerPoint PPT Presentation

TYPES OF PROGRAMMING LANGUAGES PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2019 Dalhousie University 1/30 REASONS TO CHOOSE A PARTICULAR PROGRAMMING LANGUAGE Easy to express complex ideas Easy to control exactly how the


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. • 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

  11. • 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. INTERMISSION: SCHEME AND PROLOG TUTORIALS 11/30

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

  51. 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

  52. 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

  53. 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

  54. 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

  55. 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

  56. 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

  57. 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

  58. 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

  59. 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

  60. 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

  61. 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

  62. 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

  63. 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

  64. 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

  65. 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

  66. 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

  67. 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

  68. 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

  69. 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

  70. 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

  71. 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

  72. 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

  73. 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

  74. 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

  75. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend