Skip to main content
almarefa.net

Back to all posts

How to Define Functions In Haskell?

Published on
4 min read
How to Define Functions In Haskell? image

Best Haskell Programming Books to Buy in October 2025

1 Real World Haskell

Real World Haskell

BUY & SAVE
$24.40 $49.99
Save 51%
Real World Haskell
2 Learn You a Haskell for Great Good!: A Beginner's Guide

Learn You a Haskell for Great Good!: A Beginner's Guide

BUY & SAVE
$35.00 $44.95
Save 22%
Learn You a Haskell for Great Good!: A Beginner's Guide
3 Haskell in Depth

Haskell in Depth

BUY & SAVE
$57.13 $59.99
Save 5%
Haskell in Depth
4 Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

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

BUY & SAVE
$55.05 $57.95
Save 5%
Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming
5 Learn Haskell by Example (Bookcamp)

Learn Haskell by Example (Bookcamp)

BUY & SAVE
$51.84 $59.99
Save 14%
Learn Haskell by Example (Bookcamp)
6 Programming in Haskell

Programming in Haskell

BUY & SAVE
$42.99 $47.00
Save 9%
Programming in Haskell
7 Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

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

BUY & SAVE
$25.83 $44.99
Save 43%
Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming
+
ONE MORE?

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.

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:

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:

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:

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

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

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:

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:

-- 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.