How to Define Functions In Haskell?

8 minutes read

In Haskell, functions are defined using the keyword "let" followed by the function name, a list of arguments, an equal sign, and the function body. The function body specifies what the function does when applied to its arguments. Here's the general syntax:


let functionName arg1 arg2 ... = functionBody


The arguments are separated by spaces, and the function body can be any valid Haskell expression. For example, let's define a function called "square" which takes one argument and returns the square of that number:


let square x = x * x


Here, "square" is the function name and "x" is the argument. The function body is "x * x", which calculates the square of "x" by multiplying it by itself.

Best Haskell Books to Read in 2024

1
Programming in Haskell

Rating is 5 out of 5

Programming in Haskell

2
Get Programming with Haskell

Rating is 4.9 out of 5

Get Programming with Haskell

3
Haskell in Depth

Rating is 4.8 out of 5

Haskell in Depth

4
Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

Rating is 4.7 out of 5

Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

5
Programming in Haskell

Rating is 4.6 out of 5

Programming in Haskell

6
Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

Rating is 4.5 out of 5

Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

7
Haskell from the Very Beginning

Rating is 4.4 out of 5

Haskell from the Very Beginning

8
Haskell: The Craft of Functional Programming (International Computer Science Series)

Rating is 4.3 out of 5

Haskell: The Craft of Functional Programming (International Computer Science Series)


How to declare a pure function in Haskell?

In Haskell, you can declare a pure function by defining the function signature and its implementation. Here's the syntax to declare a pure function in Haskell:

1
2
functionName :: Type1 -> Type2 -> ... -> ReturnType
functionName arg1 arg2 ... = functionBody


Here's an example of declaring a pure function sumSquare that takes two integers and returns the sum of their squares:

1
2
sumSquare :: Int -> Int -> Int
sumSquare x y = (x * x) + (y * y)


In this example, the function sumSquare takes two arguments x and y of type Int and returns an Int. The function body simply calculates the sum of the squares of x and y.


Pure functions are referentially transparent, meaning that their output is solely determined by their input and they don't have any side effects. Consequently, they always produce the same output for the same input.


How to declare a function in Haskell?

To declare a function in Haskell, you can use the following syntax:

1
2
functionName :: argType1 -> argType2 -> ... -> returnType
functionName arg1 arg2 ... = functionBody


Here's an example of declaring a simple function that adds two numbers:

1
2
add :: Int -> Int -> Int
add x y = x + y


In this example, the function add takes two arguments of type Int and returns an Int. The function body is x + y, which adds the two arguments together.


You can call this function by providing the appropriate arguments:

1
2
3
4
main :: IO ()
main = do
  let result = add 3 4
  putStrLn $ "Result: " ++ show result


In this example, add 3 4 returns 7, which is then printed to the console.


Note: Haskell is a statically typed language, so you need to specify the types of function arguments and the return type.


How to define a function with default arguments in Haskell?

In Haskell, you can define a function with default arguments by using the concept of currying. Here's an example of defining a function with default arguments:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
-- Define a function with default arguments
myFunction :: Int -> Int -> Int -> Int
myFunction x y z = x + y + z

-- Call the function with default arguments
result1 = myFunction 1 2 3   -- Output: 6

-- Define a partially applied function with default arguments
myPartialFunction :: Int -> Int -> Int
myPartialFunction = myFunction 1

-- Call the partially applied function with default arguments
result2 = myPartialFunction 2 3   -- Output: 6


In this example, myFunction is a function that takes three Int arguments and returns their sum. The values of x, y, and z can be provided explicitly when calling the function.


You can also define a partially applied version of myFunction called myPartialFunction by providing a default value for x. Since x has a default value of 1, you only need to provide values for y and z when calling myPartialFunction.


Note that Haskell functions are normally defined without default arguments. If you need a function with default arguments, it is usually recommended to use currying to achieve the same effect.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

Higher-order functions in Haskell allow functions to take other functions as arguments or return functions as results. This feature enables powerful abstractions and expressive coding in Haskell.To work with higher-order functions in Haskell, you can define fu...
Unit testing in Haskell is an essential part of the development process to ensure the correctness and reliability of code. Here's an overview of how to perform unit testing in Haskell.Import Necessary Libraries: First, you need to import the necessary test...
Creating a simple web application in Haskell involves a few key steps:Setting up your development environment: Install Haskell on your system along with any necessary libraries you may need for web development. This typically includes the Haskell Platform, whi...