Guest User

Untitled

a guest
Dec 13th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. {-
  2. Example of a function declaration.
  3. Type declaration defines types of parameters and a result.
  4.  
  5. Parameters and a returned result are separated by an arrow (i.e. ->).
  6. The result is the last one, all the rest are parameters.
  7.  
  8. This function calculates sum of parameters.
  9. -}
  10. functionExample :: Int -> Int -> Int
  11. functionExample x y =
  12. x + y
  13.  
  14. {-
  15. Example of a function that is using another function.
  16. The function uses functionExample to calculate sum.
  17.  
  18. Calculates the square of the sum of parameters.
  19. -}
  20. useAnother :: Int -> Int -> Int
  21. useAnother x y =
  22. (functionExample x y) * (functionExample x y)
  23.  
  24. {-
  25. Example of using if statement
  26.  
  27. Tells whether the parameter is equal to five.
  28. -}
  29. -- if CONDITION then EXPR1 else EXPR2
  30. ifExample :: Int -> String
  31. ifExample number =
  32. if number == 5
  33. then "It is five."
  34. else "It is not five. It is " ++ show number
  35.  
  36.  
  37. {-
  38. Example of using nested if statements.
  39. Nested ifs make it hard to read code.
  40.  
  41. Tells whether the parameter is equal to 1000, 500, 100, or 10.
  42. -}
  43. ifComplexExample :: Int -> String
  44. ifComplexExample number =
  45. if number == 1000
  46. then "it is 1000"
  47. else if number == 500
  48. then "it is 500"
  49. else if number == 100
  50. then "it is 100"
  51. else if number == 10
  52. then "it is 10"
  53. else "I don't know"
  54.  
  55. {-
  56. Example of using case statement.
  57. case makes it easier to read code.
  58.  
  59. Tells whether the parameter is equal to 1000, 500, 100, or 10.
  60. -}
  61. -- case EXPRESSION of
  62. -- PATTERN1 -> EXPR1
  63. -- PATTERN2 -> EXPR2
  64. -- ...
  65. -- PATTERNn -> EXPRn
  66. -- _ -> COMMON_EXPR
  67. caseExample :: Int -> String
  68. caseExample number =
  69. case number of
  70. 1000 -> " 1000"
  71. 500 -> "it is 500"
  72. 100 -> "it is 100"
  73. 10 -> "it is 10"
  74. _ -> "I don't know"
  75.  
  76.  
  77. {-
  78. Underscore is used as parameter name if we don't care about its value.
  79. Compiler does not care about the name of params, but it makes code easier to read.
  80.  
  81. Always returns first passed parameter as a result.
  82. -}
  83. underscoreExample :: Int -> Int -> Int
  84. underscoreExample number _ =
  85. number
  86.  
  87. {-
  88. Example of using let statement.
  89. Using "let" allows to avoid code duplication and magic numbers. Can be used to store some intermediate calculation result.
  90.  
  91. Calculates the square of a difference between parameters.
  92. -}
  93. -- let DECLARATIONS in EXPRESSION
  94. letExample :: Int -> Int -> Int
  95. letExample x y =
  96. let diff = x - y
  97. sum = x + y
  98. in
  99. diff * diff
  100.  
  101. {-
  102. Example of using where statement.
  103. where is an alternative to let.
  104. Both let and where can be used in the same function declaration, but it can make code harder to read.
  105.  
  106. Calculates the square of a difference between parameters.
  107. -}
  108. whereExample :: Int -> Int -> Int
  109. whereExample x y =
  110. diff * diff
  111. where
  112. diff = x - y
  113.  
  114. {-
  115. Examples of lists
  116. -}
  117. emptyList = []
  118. numbersList = [1, 2, 3, 4]
  119. stringsList = ["One", "Two", "Three"]
  120.  
  121. -- ["1", 1] -- will fail during compilation, as all the values in the list should be of the same type.
  122.  
  123. -- example of a type declaration for a list
  124. numbers :: [Int]
  125. numbers = [1, 2, 3]
  126.  
  127. {-
  128. Example of a recursion. If number is not equal to 0, function calls itself with a number minus one.
  129. There are no cycles in functional programming, recursion is used instead.
  130.  
  131. Calculates the factorial of a number.
  132. -}
  133. recursionExample1 :: Int -> Int
  134. recursionExample1 number =
  135. if number == 1
  136. then 0
  137. else number * recursionExample1 (number - 1)
  138.  
  139. {-
  140. The same recursion function, but with "case ... of" statement instead of "if then else"
  141.  
  142. Calculates the factorial of a number.
  143. -}
  144. recursionExample2 :: Int -> Int
  145. recursionExample2 number =
  146. case number of
  147. 0 -> 1
  148. number -> number * recursionExample2 (number - 1)
  149.  
  150. {-
  151. The same recursion function, but with pattern matching instead of "if then else"
  152.  
  153. Calculates the factorial of a number.
  154. -}
  155. recursionExample3 :: Int -> Int
  156. recursionExample3 0 = 1
  157. recursionExample3 number = number * recursionExample3 (number - 1)
Add Comment
Please, Sign In to add comment