Skip to content

Instantly share code, notes, and snippets.

@dmitryrogozhny
Created December 11, 2017 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryrogozhny/cc4a7df7041c713dae08a43e3c5c6c3b to your computer and use it in GitHub Desktop.
Save dmitryrogozhny/cc4a7df7041c713dae08a43e3c5c6c3b to your computer and use it in GitHub Desktop.
{-
Example of a function declaration.
Type declaration defines types of parameters and a result.
Parameters and a returned result are separated by an arrow (i.e. ->).
The result is the last one, all the rest are parameters.
This function calculates sum of parameters.
-}
functionExample :: Int -> Int -> Int
functionExample x y =
x + y
{-
Example of a function that is using another function.
The function uses functionExample to calculate sum.
Calculates the square of the sum of parameters.
-}
useAnother :: Int -> Int -> Int
useAnother x y =
(functionExample x y) * (functionExample x y)
{-
Example of using if statement
Tells whether the parameter is equal to five.
-}
-- if CONDITION then EXPR1 else EXPR2
ifExample :: Int -> String
ifExample number =
if number == 5
then "It is five."
else "It is not five. It is " ++ show number
{-
Example of using nested if statements.
Nested ifs make it hard to read code.
Tells whether the parameter is equal to 1000, 500, 100, or 10.
-}
ifComplexExample :: Int -> String
ifComplexExample number =
if number == 1000
then "it is 1000"
else if number == 500
then "it is 500"
else if number == 100
then "it is 100"
else if number == 10
then "it is 10"
else "I don't know"
{-
Example of using case statement.
case makes it easier to read code.
Tells whether the parameter is equal to 1000, 500, 100, or 10.
-}
-- case EXPRESSION of
-- PATTERN1 -> EXPR1
-- PATTERN2 -> EXPR2
-- ...
-- PATTERNn -> EXPRn
-- _ -> COMMON_EXPR
caseExample :: Int -> String
caseExample number =
case number of
1000 -> " 1000"
500 -> "it is 500"
100 -> "it is 100"
10 -> "it is 10"
_ -> "I don't know"
{-
Underscore is used as parameter name if we don't care about its value.
Compiler does not care about the name of params, but it makes code easier to read.
Always returns first passed parameter as a result.
-}
underscoreExample :: Int -> Int -> Int
underscoreExample number _ =
number
{-
Example of using let statement.
Using "let" allows to avoid code duplication and magic numbers. Can be used to store some intermediate calculation result.
Calculates the square of a difference between parameters.
-}
-- let DECLARATIONS in EXPRESSION
letExample :: Int -> Int -> Int
letExample x y =
let diff = x - y
sum = x + y
in
diff * diff
{-
Example of using where statement.
where is an alternative to let.
Both let and where can be used in the same function declaration, but it can make code harder to read.
Calculates the square of a difference between parameters.
-}
whereExample :: Int -> Int -> Int
whereExample x y =
diff * diff
where
diff = x - y
{-
Examples of lists
-}
emptyList = []
numbersList = [1, 2, 3, 4]
stringsList = ["One", "Two", "Three"]
-- ["1", 1] -- will fail during compilation, as all the values in the list should be of the same type.
-- example of a type declaration for a list
numbers :: [Int]
numbers = [1, 2, 3]
{-
Example of a recursion. If number is not equal to 0, function calls itself with a number minus one.
There are no cycles in functional programming, recursion is used instead.
Calculates the factorial of a number.
-}
recursionExample1 :: Int -> Int
recursionExample1 number =
if number == 1
then 0
else number * recursionExample1 (number - 1)
{-
The same recursion function, but with "case ... of" statement instead of "if then else"
Calculates the factorial of a number.
-}
recursionExample2 :: Int -> Int
recursionExample2 number =
case number of
0 -> 1
number -> number * recursionExample2 (number - 1)
{-
The same recursion function, but with pattern matching instead of "if then else"
Calculates the factorial of a number.
-}
recursionExample3 :: Int -> Int
recursionExample3 0 = 1
recursionExample3 number = number * recursionExample3 (number - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment