How to Define A Function In Haskell?

9 minutes read

In Haskell, functions are defined using the syntax name arguments = functionBody. Here, name represents the name of the function, arguments represents the input parameters the function takes, and functionBody defines the computation the function performs.


Haskell is a statically-typed language, so you need to specify the type of the function and its arguments. This can be done through type annotations. For example, a function that takes two Integers and returns their sum can be defined as follows:

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


In this example, the function add takes two Integer arguments, x and y, and returns their sum using the + operator.


You can define a function without any arguments as well. For instance, a function that simply returns a predefined value can be defined as:

1
2
helloWorld :: String
helloWorld = "Hello, World!"


Here, the function helloWorld doesn't take any arguments and returns a value of type String, which is the classic "Hello, World!" message.


Haskell supports pattern matching, allowing you to define functions with multiple patterns. Each pattern corresponds to a different computation. For example, a simple factorial function can be defined using pattern matching as shown below:

1
2
3
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)


In this case, the factorial function pattern matches with 0 and returns 1. For any other value, it recursively multiplies the current value with the factorial of n-1 until it reaches the base case.

Best Haskell Books to Read in 2024

1
Haskell in Depth

Rating is 5 out of 5

Haskell in Depth

2
Programming in Haskell

Rating is 4.9 out of 5

Programming in Haskell

3
Get Programming with Haskell

Rating is 4.8 out of 5

Get Programming with Haskell

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
Haskell from the Very Beginning

Rating is 4.6 out of 5

Haskell from the Very Beginning

6
The Haskell School of Expression: Learning Functional Programming through Multimedia

Rating is 4.5 out of 5

The Haskell School of Expression: Learning Functional Programming through Multimedia


What is the syntax for defining an anonymous function in Haskell?

In Haskell, anonymous functions are defined using the "" (backslash) symbol. The general syntax for defining an anonymous function is as follows:

1
(\parameter1 parameter2 ... -> expression)


Here, parameter1, parameter2, etc., represent the parameters of the function, and expression represents the body of the function.


For example, an anonymous function that takes two arguments x and y, and returns their sum, can be defined as:

1
(\x y -> x + y)


You can use the anonymous function immediately, or assign it to a variable for later use. For example, you can use it directly in a function call:

1
(\x y -> x + y) 3 5


Or you can assign it to a variable and use it later:

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


In this case, the anonymous function is assigned to the add variable, which can then be used like any other named function.


What are infix functions, and how do we define them in Haskell?

Infix functions in Haskell refer to functions that are used in infix notation, which means they are placed between their arguments. For example, the addition operator "+" is an infix function that takes two arguments.


To define an infix function in Haskell, we enclose its name in backticks (`) in the function definition. This allows us to use the function in infix notation. We can also define the associativity and precedence of an infix function using parentheses.


Here's an example of defining an infix function called "add" that adds two numbers:

1
2
add :: Int -> Int -> Int
add a b = a + b


In the above code, the function "add" takes two arguments of type "Int" and returns an "Int" value. The "+" operator is used in the function body to perform the addition.


Once defined, we can use the infix function in infix notation:

1
result = 5 `add` 3


In this example, the "add" function is used in infix notation between the two arguments 5 and 3, giving us a result of 8.


How to write a basic function in Haskell?

To write a basic function in Haskell, you can follow these steps:

  1. Open your preferred text editor or integrated development environment (IDE) that supports Haskell.
  2. Open a new file and save it with a .hs extension, for example, basicFunction.hs.
  3. Start by declaring the type of your function. This is optional but recommended, as it helps to avoid errors. For example, to define a function that takes two integers and returns their sum, you would write: myFunction :: Int -> Int -> Int
  4. Write the function definition, specifying the function name and its parameters. For example: myFunction x y = x + y
  5. Save the file.


Here's the complete example:

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


Once you have defined your function, you can load the file into an interactive Haskell interpreter like GHCi by typing ghci basicFunction.hs in your terminal. You can then call the function within GHCi or use it in other Haskell files.


How to use the 'let' keyword to define functions in Haskell?

In Haskell, you can use the let keyword to define functions locally within another function. Here's an example of how to use let to define a function:

1
2
3
4
5
myFunction :: Int -> Int
myFunction x = 
    let y = x * 2     -- Define a local variable y
        z = y + 1     -- Define another local variable z
    in z             -- Return the value of z


In this example, myFunction takes an Int as input and returns an Int. Inside the function, we use the let keyword to introduce local bindings for the variables y and z. The value of y is x multiplied by 2, and the value of z is y plus 1. Finally, we use the in keyword to specify the expression to be returned from the function, which in this case is z.


You can also define local functions using let. Here's an example of defining a local function within another function:

1
2
3
4
5
myFunction :: Int -> Int
myFunction x = 
    let square y = y * y   -- Define a local function square
        result = square x  -- Call the local function with x as argument
    in result             -- Return the value of result


In this example, a local function square is defined within myFunction. square takes an argument y and returns its square. myFunction then calls square with x as the argument and assigns the result to result. Finally, result is returned as the result of myFunction.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Function evaluation in Haskell follows a process known as lazy evaluation. Unlike eager evaluation, which immediately evaluates all expressions, lazy evaluation delays evaluation until the value is actually needed. This leads to increased efficiency by avoidin...
To "overload" a dollar operator in Haskell, you need to define a new operator that behaves differently than the regular function application operator $.In Haskell, the $ operator is right associative and has the lowest precedence, allowing you to avoid...
To write a recursive function in Haskell, you can follow these steps:Define the base case: Start by defining the simplest case for which you can compute the result directly without recursion. This will serve as the termination condition for the recursion. Defi...