HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to HaskellVariables (let)Data TypesOperatorsStringsListsConditions (if / then / else)Iteration (map / sum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Value & Type
🔢 Numbers & Math
✍️ String
📚 List
🔀 Condition
🧩 Function
Haskell›Functions

Functions

Function definition, type signature (::), and calling (without parentheses, using spaces).

Simple enough for a 5-year-old to understand

Define a function

The function is written in the format "name parameter = expression". A type signature is placed above it.

Example code
1
square :: Int -> Int
Type signature: takes an Int, returns an Int.
FunctionType signature: takes an Int, returns an Int.
2
square x = x * x
Definition.
FunctionDefinition.
3
main = print (square 5)
25 (called with a space).
Output / Input25 (called with a space).
FunctionOutput / Input
💡 Hint: Function calls are without parentheses: square 5 (not square(5)).

About this topic

Defining a function in Haskell is like writing an equation: name and parameters on the left, result on the right. There is no return statement because the body of the function is the result itself.

Haskell functions are curried: a function with two arguments actually takes one argument and returns a new function. Therefore, you can do partial application by supplying some of the arguments, which significantly shortens the code.

Pattern matching is part of the function definition. You define the same function on separate lines for different input formats; Haskell uses the first matching definition from top to bottom. That is why the order matters.

What you will learn

  • Function defining
  • Writing a type signature
  • Pattern matching and its order
  • Partial application
  • Anonymous functions

Who is this for?

  • Beginners to Haskell
  • Those trying to resolve type errors
  • Learners of the currying concept

When to use?

If a part of a calculation appears in multiple places, it should be extracted into a function; in Haskell this is very cheap.

Partial application is the shortest way to produce a version of the same function used with a specific setting.

Common mistakes

Wrong

kontrol :: Int -> String
kontrol _ = "other"
kontrol 0 = "zero"

Patterns are tried from top to bottom, and since the general pattern is in the first position, the second definition is never reached; the compiler gives the warning "Pattern match is redundant". Special cases must always be written before the general case.

Correct

kontrol :: Int -> String
kontrol 0 = "zero"
kontrol _ = "other"

Wrong

topla :: Int -> Int
topla a b = a + b

The type signature declares a single-argument function but the definition takes two arguments; the error "The equation for add has two arguments, but its type has only one" is thrown. Each argument must be indicated with a separate arrow in the signature.

Correct

topla :: Int -> Int -> Int
topla a b = a + b

Wrong

main = do
  let iki = map (* 2)
  print (iki 5)

The partially applied function expects a list, not a single number; a type error is thrown. The mapping function always works on a list. There is no need for mapping to double a single value.

Correct

main = do
  let iki = (* 2)
  print (iki 5)

Frequently asked questions

Why is currying useful?

It allows you to produce specialized functions by pre-supplying some of the arguments. Thus, you can create new behaviors by combining existing functions without writing an anonymous function; the code becomes both shorter and more readable.

When is an anonymous function used?

In short transformations needed in only one place. If it exceeds a few lines or will be used a second time, it is better to give it a name; a name is the cheapest description documenting what a function does.

How do type signatures affect error messages?

Significantly improves them. If there is no signature, the compiler infers the type from the usage sites, and the error may appear far away from the actual problem. When you write a signature, the error is reported at the definition itself.

Summary

Functions are defined in equation format.

Patterns are tried from top to bottom.

The catch-all pattern must be at the very end.

Each argument is indicated with a separate arrow in the signature.

Partial application produces a new function.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code