What is a Haskell function?
Overview
Haskell’s is a purely functional language and functions are its primary aspect.
Haskell syntax for declaring functions is as follows:
- Function name
- List of arguments
- Outputs
The function definition is where you place the logic of the function.
Code
Let’s see the syntax in the code below.
multiplication :: Integer -> Integer -> Integer --function Instantiationmultiplication a b = a * b --function definition/logicaddition :: Integer -> Integer -> Integer --function Instantiationaddition d f = d + fmain = doprint(multiplication 3 9) --calling the functionprint(addition 3 9)
Explanation
-
In the example above, we instantiate our function in the first line and define it in the second line.
-
Next, we place the main logic of the function. In other words, we have written that the function takes two arguments and produces one integer type output.
-
We did the same for the second function,
addition, you will notice that when printing the result of themultiplicationandadditionfunction, they both work. This shows that theadditionfunction will run without affecting themultiplicationfunction unless theadditionfunction throws an error while executing. -
The code will start executing from the
mainmethod.
To see the output, run the code above.