Updated April 19, 2023
Introduction to Haskell function
In Haskell, we have different functions available also we can write our own functions which are responsible to perform some specific tasks for us. It is good practice to write the functions in any programming language because they can be reusable, also if we write functions for any specific logic then it is very clear and easy to understand as well. Also, we can use the same function performing the logic a different number of times. In the coming section of the tutorial, we will see the internal working and its standard to define the function in Haskell in detail for better understanding for the beginners and start using it for better quality and performance of the code.
As we have discussed that to define a function in Haskell we have to follow some stander given by the Haskell official documentation which helps us to get the desired result as well, let’s take a closer look at the function declaration in Haskell for functions see below;
function_name :: list of arguments
As you can see in the above line of syntax for function in Haskell, we first give the name followed by the list of arguments it takes and produces the output for us. Let’s taken a closer look at the practice syntax for function in Haskell for beginners which will give a clear understanding see below;
demo :: Float -> Float
As you can see in the above line of syntax we have given the function name as ‘demo’ after this we can define its body as well but this we will see in the coming section of the tutorial in detail for a better understanding, also we will some practical example to get the better idea for its implementation.
How to create a function in Haskell?
As we already know that Haskell is a functional programming language, Haskell provides us many inbuilt functions which help us to perform the basic operations we need. Also, we can define our own functions as well which will perform the desired operations for us. These functions are re-usable which can be called as many times we want to perform the same logic more than once. This will reduce the duplicate code, reduce the line of code, and also the code will be easy to understand and handle by the developers. In this section, we will first talk about the function declaration and its definition in detail which will help us to get started with it. See below;
1) Function declaration in Haskell: first we will see the function declaration in Haskell, which will define what type of parameter it will take, also the number of parameters, and output it will produce. In this section we will first see the function declaration syntax to understand it better see below;
function_name :: param 1 -> param 2 -> param 3
As you can see in the above line of code for function declaration in Haskell, it is very easy to declare and understand. First, we have provided a unique name to the function by which it will be accessible in the code. After that we have defined the type of parameter it will take and the output it will produce, Let’s take a closer look below;
demofun :: String -> String -> String
As you can see in the above line of code we have defined the function which will take a string as the input param and produce the string as the output. Sin the coming section we will see the definition of the function given above.
2) Function definition: Once we defined the function then we have to give its body means the definition of the function, how it will work, and provide us the desired result when called. So for that, we have to give one proper definition of our function which will help us to get the output. Let’s take a closer look at the syntax or it, see below;
demofun a b = a ++ b
As you can see in the above line of code we are adding two variable which is being passed to ‘demofun’ in Haskell. Here we are consuming string and producing the string as the result here as well. We can also call this function wherever needed in the code. This will help us to reuse the same code any number of times, and reduce the complexity of the code as well.
Points to be remembered while using a function in Haskell which are as follows see below;
1) We have many in-built functions available in Haskell, but we can define our custom function as well.
2) To define any function we have to use and follow the standard given by the Haskell official documentation.
3) TO define a function make sure it’s first declared properly otherwise we will receive many errors in the code.
In this example we are creating our own function in Haskell and trying to calculate the values from it. This function will take an integer and return us the integer as the result. this function will simply add the two numbers here. this is a sample example for beginners to understand it better and start using it for better code quality after function implementation.
demofun :: Integer -> Integer -> Integer demofun a b = a + b main = do print("Demo example to show function in Haskell !!") print("calling the function here to calculate value !!") let result1 = demofun 10 20 let result2 = demofun 30 40 let result3 = demofun 500 600 let result4 = demofun 100 2000 let result5 = demofun 1 2 let result6 = demofun 67 54 let result7 = demofun 56 90 print("Printing the result here :: ") print("result one is ::", result1) print("result two is ::", result2) print("result three is ::", result3) print("result four is ::", result4) print("result five is ::", result5) print("result six ::", result6) print("result seven is ::", result7)
By using the function we can format our code, make it more readable and reusable by the developers. Also, it increases the performance and maintains of the application if used properly. By the function, we can easily bifurcate our code, which will be clean and easy to understand as well.
We hope that this EDUCBA information on “Haskell function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.