pure functional programming
play

Pure Functional Programming Functional Programming and Reasoning Dr - PowerPoint PPT Presentation

Pure Functional Programming Functional Programming and Reasoning Dr Hans Georg Schaathun University of Surrey Spring 2010 Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 1 / 52 Outline Testing and Proof 1 Pure Functional


  1. Testing and Proof A little logic A little logic Consider a statement such as Sheep are white 1 Therefore, Dolly the Sheep is white 2 This is an implication There is an assumption (1) and a consequence (2) The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

  2. Testing and Proof A little logic A little logic Consider a statement such as Sheep are white 1 Therefore, Dolly the Sheep is white 2 This is an implication There is an assumption (1) and a consequence (2) The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

  3. Testing and Proof A little logic A little logic Consider a statement such as Sheep are white 1 Therefore, Dolly the Sheep is white 2 This is an implication There is an assumption (1) and a consequence (2) The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

  4. Testing and Proof A little logic A little logic Consider a statement such as Sheep are white 1 Therefore, Dolly the Sheep is white 2 This is an implication There is an assumption (1) and a consequence (2) The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

  5. Testing and Proof A little logic A little logic Consider a statement such as Sheep are white 1 Therefore, Dolly the Sheep is white 2 This is an implication There is an assumption (1) and a consequence (2) The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

  6. Testing and Proof A little logic Logical Notation Sheep are white ⇒ Dolly the Sheep is white Dolly the Sheep is white ⇐ Sheep are white Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 15 / 52

  7. Testing and Proof A little logic Logical Notation Sheep are white ⇒ Dolly the Sheep is white Dolly the Sheep is white ⇐ Sheep are white Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 15 / 52

  8. Testing and Proof A little logic Checking the Validity Two separate questions Is the assumption (1) true? 1 Is the inference ( ⇒ ) valid? 2 The consequence (2) follows if both answers are yes Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 16 / 52

  9. Testing and Proof A little logic Deduction Sheep are white ⇒ Dolly the Sheep is white Example of Deduction i.e. a valid inference W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W Hence d ∈ W Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

  10. Testing and Proof A little logic Deduction Sheep are white ⇒ Dolly the Sheep is white Example of Deduction i.e. a valid inference W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W Hence d ∈ W Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

  11. Testing and Proof A little logic Deduction Sheep are white ⇒ Dolly the Sheep is white Example of Deduction i.e. a valid inference W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W Hence d ∈ W Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

  12. Testing and Proof A little logic Deduction Sheep are white ⇒ Dolly the Sheep is white Example of Deduction i.e. a valid inference W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W Hence d ∈ W Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

  13. Testing and Proof A little logic Deduction Sheep are white ⇒ Dolly the Sheep is white Example of Deduction i.e. a valid inference W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W Hence d ∈ W Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

  14. Testing and Proof A little logic Deduction Sheep are white ⇒ Dolly the Sheep is white Example of Deduction i.e. a valid inference W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W Hence d ∈ W Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

  15. Testing and Proof A little logic Deduction Sheep are white ⇒ Dolly the Sheep is white Example of Deduction i.e. a valid inference W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W Hence d ∈ W Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

  16. Testing and Proof A little logic Induction Generalisation for a special case Dolly the Sheep is white ⇒ Sheep are white This would be an invalid inference (generalising from a special case) Known as induction Attempts to generalise from specific information Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 18 / 52

  17. Testing and Proof A little logic Induction Generalisation for a special case Dolly the Sheep is white ⇒ Sheep are white This would be an invalid inference (generalising from a special case) Known as induction Attempts to generalise from specific information Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 18 / 52

  18. Testing and Proof A little logic Induction Generalisation for a special case Dolly the Sheep is white ⇒ Sheep are white This would be an invalid inference (generalising from a special case) Known as induction Attempts to generalise from specific information Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 18 / 52

  19. Testing and Proof A little logic Learning by Observation Dolly is white Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 19 / 52

  20. Testing and Proof A little logic Learning by Observation All (?) sheep are white Not quite. Many sheep are white. Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 20 / 52

  21. Testing and Proof A little logic Learning by Observation All (?) sheep are white Not quite. Many sheep are white. Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 20 / 52

  22. Testing and Proof A little logic Learning by Observation Not all sheep are white Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 21 / 52

  23. Testing and Proof A little logic Learning by Observation Conclusion Observable. A white sheep exists. Not observable. All sheep are white. Observable. A non-white sheep exists. Observable. Not all sheep are white. Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

  24. Testing and Proof A little logic Learning by Observation Conclusion Observable. A white sheep exists. Not observable. All sheep are white. Observable. A non-white sheep exists. Observable. Not all sheep are white. Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

  25. Testing and Proof A little logic Learning by Observation Conclusion Observable. A white sheep exists. Not observable. All sheep are white. Observable. A non-white sheep exists. Observable. Not all sheep are white. Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

  26. Testing and Proof A little logic Learning by Observation Conclusion Observable. A white sheep exists. Not observable. All sheep are white. Observable. A non-white sheep exists. Observable. Not all sheep are white. Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

  27. Testing and Proof Testing and induction Outline Testing and Proof 1 Verifying programs A little logic Testing and induction Reason and deduction Pure Functional Programming 2 Definedness and Termination 3 Conclusion 4 Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 23 / 52

  28. Testing and Proof Testing and induction Testing does special cases only Program works for input dolly 1 Program works for any input (of type Sheep ) 2 The former is observable – by testing The latter cannot be tested And the induction ( 1 ) → ( 2 ) is not valid Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 24 / 52

  29. Testing and Proof Testing and induction Choosing test cases Black Box Testing Testing cannot prove that the program works but it can prove that it does not find the black sheep in the flock Choosing test cases is a key skill appropriate broad selection maximises the chances of finding a black sheep Black Box Testing choose test cases based on the specification with no knowledge of the implementation Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

  30. Testing and Proof Testing and induction Choosing test cases Black Box Testing Testing cannot prove that the program works but it can prove that it does not find the black sheep in the flock Choosing test cases is a key skill appropriate broad selection maximises the chances of finding a black sheep Black Box Testing choose test cases based on the specification with no knowledge of the implementation Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

  31. Testing and Proof Testing and induction Choosing test cases Black Box Testing Testing cannot prove that the program works but it can prove that it does not find the black sheep in the flock Choosing test cases is a key skill appropriate broad selection maximises the chances of finding a black sheep Black Box Testing choose test cases based on the specification with no knowledge of the implementation Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

  32. Testing and Proof Testing and induction Choosing test cases Black Box Testing Testing cannot prove that the program works but it can prove that it does not find the black sheep in the flock Choosing test cases is a key skill appropriate broad selection maximises the chances of finding a black sheep Black Box Testing choose test cases based on the specification with no knowledge of the implementation Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

  33. Testing and Proof Testing and induction Example: minThree minThree :: Int -> Int -> Int -> Int Returns the smallest of three integers Which inputs do we have to test? Three different numbers (small,middle,large); (small,large,middle); (large,middle,small); etc. Two equal + one smaller Two equal + one larger Three equal At least one test case from each category not so important to test both ( 1 , 2 , 3 ) and ( 1 , 2 , 4 ) Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 26 / 52

  34. Testing and Proof Testing and induction Example: minThree minThree :: Int -> Int -> Int -> Int Returns the smallest of three integers Which inputs do we have to test? Three different numbers (small,middle,large); (small,large,middle); (large,middle,small); etc. Two equal + one smaller Two equal + one larger Three equal At least one test case from each category not so important to test both ( 1 , 2 , 3 ) and ( 1 , 2 , 4 ) Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 26 / 52

  35. Testing and Proof Testing and induction Example: minThree minThree :: Int -> Int -> Int -> Int Returns the smallest of three integers Which inputs do we have to test? Three different numbers (small,middle,large); (small,large,middle); (large,middle,small); etc. Two equal + one smaller Two equal + one larger Three equal At least one test case from each category not so important to test both ( 1 , 2 , 3 ) and ( 1 , 2 , 4 ) Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 26 / 52

  36. Testing and Proof Reason and deduction Outline Testing and Proof 1 Verifying programs A little logic Testing and induction Reason and deduction Pure Functional Programming 2 Definedness and Termination 3 Conclusion 4 Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 27 / 52

  37. Testing and Proof Reason and deduction Analyse it Go general Testing and evaluation consider special cases Analytical argument aim to generalise Good argument is about being structured and systematic covering all cases This is about practice and talent so let’s go through an example Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 28 / 52

  38. Testing and Proof Reason and deduction Analyse it Go general Testing and evaluation consider special cases Analytical argument aim to generalise Good argument is about being structured and systematic covering all cases This is about practice and talent so let’s go through an example Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 28 / 52

  39. Testing and Proof Reason and deduction Analyse it Go general Testing and evaluation consider special cases Analytical argument aim to generalise Good argument is about being structured and systematic covering all cases This is about practice and talent so let’s go through an example Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 28 / 52

  40. Testing and Proof Reason and deduction Analyse it A sample function implementation Is the following a correct calculation of maximum? minThree x y z | x < y && x < z = x | y < x && y < z = y | otherwise = z Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 29 / 52

  41. Testing and Proof Reason and deduction Analyse it Continued 1 | x < y && x < z = x Clearly, x is strictly smaller than y and z . Correct. 2 | y < x && y < z = y Clearly, x is strictly smaller than x and z . Correct. Which cases remain for otherwise ? Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 30 / 52

  42. Testing and Proof Reason and deduction Analyse it Continued 1 | x < y && x < z = x Clearly, x is strictly smaller than y and z . Correct. 2 | y < x && y < z = y Clearly, x is strictly smaller than x and z . Correct. Which cases remain for otherwise ? Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 30 / 52

  43. Testing and Proof Reason and deduction Analyse it Continued 1 | x < y && x < z = x Clearly, x is strictly smaller than y and z . Correct. 2 | y < x && y < z = y Clearly, x is strictly smaller than x and z . Correct. Which cases remain for otherwise ? Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 30 / 52

  44. Testing and Proof Reason and deduction Analyse it The otherwise cases We may have z < x and z < y , 1 when z is the correct output. We may have x = y = z , 2 when z is correct (as would x or y be) Cases with two equal inputs 3 ... next slide ... Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 31 / 52

  45. Testing and Proof Reason and deduction Analyse it The otherwise cases We may have z < x and z < y , 1 when z is the correct output. We may have x = y = z , 2 when z is correct (as would x or y be) Cases with two equal inputs 3 ... next slide ... Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 31 / 52

  46. Testing and Proof Reason and deduction Analyse it The otherwise cases We may have z < x and z < y , 1 when z is the correct output. We may have x = y = z , 2 when z is correct (as would x or y be) Cases with two equal inputs 3 ... next slide ... Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 31 / 52

  47. Testing and Proof Reason and deduction Analyse it Two equal inputs z = x z ≤ y : otherwise applies – correct z > y : Case 2 applies – correct z = y z ≤ x : otherwise applies – correct z > x : Case 1 applies – correct x = y z ≤ x : otherwise applies – correct z > x : otherwise applies – minimum is x (= y ) Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 32 / 52

  48. Testing and Proof Reason and deduction Analyse it Two equal inputs z = x z ≤ y : otherwise applies – correct z > y : Case 2 applies – correct z = y z ≤ x : otherwise applies – correct z > x : Case 1 applies – correct x = y z ≤ x : otherwise applies – correct z > x : otherwise applies – minimum is x (= y ) Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 32 / 52

  49. Testing and Proof Reason and deduction Analyse it Two equal inputs z = x z ≤ y : otherwise applies – correct z > y : Case 2 applies – correct z = y z ≤ x : otherwise applies – correct z > x : Case 1 applies – correct x = y z ≤ x : otherwise applies – correct z > x : otherwise applies – minimum is x (= y ) Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 32 / 52

  50. Testing and Proof Reason and deduction Analysis Summary We found the bug by carefully checking every case, class by class the classes cover all possible inputs Suppose there were no bug would this method allow us to prove correctness? Yes – we have not only found the bug we know exactly which inputs for which the output is wrong For each class of inputs, it is either correct or wrong and any possible inputs falls in at least one class Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

  51. Testing and Proof Reason and deduction Analysis Summary We found the bug by carefully checking every case, class by class the classes cover all possible inputs Suppose there were no bug would this method allow us to prove correctness? Yes – we have not only found the bug we know exactly which inputs for which the output is wrong For each class of inputs, it is either correct or wrong and any possible inputs falls in at least one class Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

  52. Testing and Proof Reason and deduction Analysis Summary We found the bug by carefully checking every case, class by class the classes cover all possible inputs Suppose there were no bug would this method allow us to prove correctness? Yes – we have not only found the bug we know exactly which inputs for which the output is wrong For each class of inputs, it is either correct or wrong and any possible inputs falls in at least one class Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

  53. Testing and Proof Reason and deduction Analysis Summary We found the bug by carefully checking every case, class by class the classes cover all possible inputs Suppose there were no bug would this method allow us to prove correctness? Yes – we have not only found the bug we know exactly which inputs for which the output is wrong For each class of inputs, it is either correct or wrong and any possible inputs falls in at least one class Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

  54. Testing and Proof Reason and deduction The bug fix How do you correct the bug in the code? (one way) replace < by <= in the guards Exercise: run through the analysis above for the corrected code are you convinced that the implementation is correct for any input? Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 34 / 52

  55. Testing and Proof Reason and deduction The bug fix How do you correct the bug in the code? (one way) replace < by <= in the guards Exercise: run through the analysis above for the corrected code are you convinced that the implementation is correct for any input? Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 34 / 52

  56. Testing and Proof Reason and deduction The bug fix How do you correct the bug in the code? (one way) replace < by <= in the guards Exercise: run through the analysis above for the corrected code are you convinced that the implementation is correct for any input? Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 34 / 52

  57. Testing and Proof Reason and deduction White Box Testing Black Box Testing consider the implementation as a black box no knowledge of the implementation White Box Testing analyse the implementation to choose test cases In particular, look at the guards when evaluation branches, use test sets for each branch This often helps adding important test cases otherwise forgotten Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

  58. Testing and Proof Reason and deduction White Box Testing Black Box Testing consider the implementation as a black box no knowledge of the implementation White Box Testing analyse the implementation to choose test cases In particular, look at the guards when evaluation branches, use test sets for each branch This often helps adding important test cases otherwise forgotten Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

  59. Testing and Proof Reason and deduction White Box Testing Black Box Testing consider the implementation as a black box no knowledge of the implementation White Box Testing analyse the implementation to choose test cases In particular, look at the guards when evaluation branches, use test sets for each branch This often helps adding important test cases otherwise forgotten Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

  60. Testing and Proof Reason and deduction White Box Testing Black Box Testing consider the implementation as a black box no knowledge of the implementation White Box Testing analyse the implementation to choose test cases In particular, look at the guards when evaluation branches, use test sets for each branch This often helps adding important test cases otherwise forgotten Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

  61. Pure Functional Programming Outline Testing and Proof 1 Pure Functional Programming 2 Definedness and Termination 3 Conclusion 4 Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 36 / 52

  62. Pure Functional Programming Pure functional language Haskell is a pure functional language therefore, lends itself well to reasoning It adhers to a strict set of principles contrary to languages providing imperative and functional features together Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 37 / 52

  63. Pure Functional Programming What is a program Functional : list of definitions one expression being evaluated at the end Imperative : list of instructions executed one by one modifying variables (system state) Time is important in imperative programming instructions are executed in order and instructions can redefine objects Definitions are constant in functional programming nothing changes over time Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 38 / 52

  64. Pure Functional Programming Referential transparency All calls to the same function same input ⇒ same output Two major benefits the compiler can optimise (avoid recomputation) the developer can make mathematical proofs This is not true for imperative programming as functions may depend on the system state (variables) such dependencies may be hard to spot, and makes reasoning more difficult Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 39 / 52

  65. Pure Functional Programming Referential transparency All calls to the same function same input ⇒ same output Two major benefits the compiler can optimise (avoid recomputation) the developer can make mathematical proofs This is not true for imperative programming as functions may depend on the system state (variables) such dependencies may be hard to spot, and makes reasoning more difficult Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 39 / 52

  66. Pure Functional Programming Referential transparency All calls to the same function same input ⇒ same output Two major benefits the compiler can optimise (avoid recomputation) the developer can make mathematical proofs This is not true for imperative programming as functions may depend on the system state (variables) such dependencies may be hard to spot, and makes reasoning more difficult Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 39 / 52

  67. Pure Functional Programming Side-effects Functions have no side-effect all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables Pure functional languages forbid side-effects completely Avoiding side-effects is good practice easier to reuse code other applications often need same return value different side-effects (output) e.g. computational back-end without IO can be combined with different user interfaces Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

  68. Pure Functional Programming Side-effects Functions have no side-effect all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables Pure functional languages forbid side-effects completely Avoiding side-effects is good practice easier to reuse code other applications often need same return value different side-effects (output) e.g. computational back-end without IO can be combined with different user interfaces Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

  69. Pure Functional Programming Side-effects Functions have no side-effect all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables Pure functional languages forbid side-effects completely Avoiding side-effects is good practice easier to reuse code other applications often need same return value different side-effects (output) e.g. computational back-end without IO can be combined with different user interfaces Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

  70. Pure Functional Programming Side-effects Functions have no side-effect all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables Pure functional languages forbid side-effects completely Avoiding side-effects is good practice easier to reuse code other applications often need same return value different side-effects (output) e.g. computational back-end without IO can be combined with different user interfaces Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

  71. Pure Functional Programming Side-effects An example function PseudoRandom() state := state * 17822228932 + 178324742 return state mod 256 You want a new pseudo-random number every time Side-effects is useful in this case Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 41 / 52

  72. Pure Functional Programming Side-effects An example function PseudoRandom() state := state * 17822228932 + 178324742 return state mod 256 You want a new pseudo-random number every time Side-effects is useful in this case Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 41 / 52

  73. Pure Functional Programming Other side-effects The most common side-effect is output This is often a bad side-effect You don’t always want the same output Some users will need the calculations without output Separation is useful Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 42 / 52

  74. Pure Functional Programming Imperative programming including Java Global state (memory) methods (functions) can change the state method behaviour can depend on state Different calls to f can give different output typical example: getchar (read keyboard input) one call removes a character from input buffer changes the value of the next call Functional principles can be used in imperative languages avoid side-effects when they are not necessary separate computations and IO Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 43 / 52

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