Skip to main content
ubuntuask.com

Back to all posts

How to Call A Function In Haskell?

Published on
4 min read
How to Call A Function In Haskell? image

Best Haskell Programming Books to Buy in October 2025

1 Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

BUY & SAVE
$35.86 $49.99
Save 28%
Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell
2 Programming in Haskell

Programming in Haskell

BUY & SAVE
$42.99 $47.00
Save 9%
Programming in Haskell
3 Learn You a Haskell for Great Good!: A Beginner's Guide

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

  • AFFORDABLE PRICES OFFER GREAT VALUE FOR BOOK LOVERS.
  • QUALITY ASSURANCE: INSPECTED FOR READABILITY AND USABILITY.
  • ECO-FRIENDLY CHOICE: SUPPORT RECYCLING AND REDUCE WASTE.
BUY & SAVE
$35.00 $44.95
Save 22%
Learn You a Haskell for Great Good!: A Beginner's Guide
4 Real World Haskell

Real World Haskell

  • QUALITY ASSURANCE: ALL BOOKS INSPECTED FOR READABILITY AND QUALITY.
  • AFFORDABLE PRICING: HIGH-QUALITY USED BOOKS AT BUDGET-FRIENDLY PRICES.
  • ECO-FRIENDLY CHOICE: SUPPORT SUSTAINABILITY BY BUYING USED BOOKS.
BUY & SAVE
$24.40 $49.99
Save 51%
Real World Haskell
5 Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up

Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up

BUY & SAVE
$45.99
Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up
6 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
7 Haskell in Depth

Haskell in Depth

BUY & SAVE
$57.13 $59.99
Save 5%
Haskell in Depth
8 Introduction to Computation: Haskell, Logic and Automata (Undergraduate Topics in Computer Science)

Introduction to Computation: Haskell, Logic and Automata (Undergraduate Topics in Computer Science)

BUY & SAVE
$26.40 $37.99
Save 31%
Introduction to Computation: Haskell, Logic and Automata (Undergraduate Topics in Computer Science)
9 Learn Haskell by Example (Bookcamp)

Learn Haskell by Example (Bookcamp)

BUY & SAVE
$51.84 $59.99
Save 14%
Learn Haskell by Example (Bookcamp)
+
ONE MORE?

In Haskell, calling a function involves providing the function name followed by the required arguments. The basic syntax to call a function is as follows:

functionName argument1 argument2 ...

Here, functionName is the name of the function you want to call, and argument1, argument2, etc. are the arguments or inputs required by the function. You can provide as many arguments as required by the function's definition.

For example, consider a simple function add that takes two numbers as arguments and returns their sum:

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

To call this add function and pass the numbers 2 and 3 as arguments, you can do:

result = add 2 3

In this case, result will hold the value 5, which is the sum of 2 and 3.

If a function takes multiple arguments, you can also partially apply it by providing fewer arguments. This results in a new function that takes the remaining arguments. For example, consider a function multiply that multiplies three numbers:

multiply :: Int -> Int -> Int -> Int multiply x y z = x * y * z

To partially apply this function and get a new function that multiplies by 2 and 3, you can do:

multiplier = multiply 2 3

Now, multiplier will be a new function that takes a single argument and returns the result of multiplying it by 2 and 3.

In Haskell, functions are typically called using currying, which means that a function that takes multiple arguments is actually a chain of functions, each taking one argument and returning a new function. This allows for easy partial application and function composition.

What happens if you call a function that is not defined in Haskell?

If you call a function in Haskell that is not defined, you will get a compilation error. Haskell uses static typing, so all functions need to be declared and defined before they can be used. If you try to call a function that has not been defined, the compiler will identify this as an error and provide a corresponding error message. This compilation error occurs because Haskell requires all functions to be defined prior to the execution of the program to ensure its correctness and consistency.

How do you pass a function as an argument in Haskell?

In Haskell, you can pass a function as an argument by specifying the function's type in the function signature and then passing the function as a parameter when calling the function.

Here is an example:

applyTwice :: (a -> a) -> a -> a applyTwice f x = f (f x)

addOne :: Int -> Int addOne x = x + 1

main :: IO () main = do let result = applyTwice addOne 5 putStrLn $ "Result: " ++ show result

In the above example, the function applyTwice takes a function f as its first argument, followed by a value x. The function type (a -> a) indicates that f takes an argument of type a and returns a value of the same type. The return type of applyTwice is also a.

In the main function, we define a function addOne that increments an integer by 1. We then call applyTwice with addOne as the first argument and 5 as the second argument. The result of applyTwice addOne 5 is 7.

Note that the function being passed as an argument must have a compatible type with the expected function type in the function signature.

Can a function in Haskell return a value?

Yes, a function in Haskell can return a value. Haskell is a pure functional language, which means that every function in Haskell must return a value. In fact, every expression in Haskell is evaluated to produce a value.