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›Iteration (map / sum)

Iteration (map / sum)

There are no loops in Haskell; repetition/transformation is done using map, sum, and ranges (1..n).

Simple enough for a 5-year-old to understand

Ranges and mapM_

[1..5] is a range (inclusive of 5). mapM_ applies an IO action to each element.

Example code
1
main = mapM_ print [1..3]
1, 2, 3 (prints each one).
Loop1, 2, 3 (prints each one).
Loop
💡 Hint: [1..5] → from 1 to 5, inclusive.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print numbers from 1 to 5 vertically using mapM_.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

There are no loops in Haskell. You achieve repetition in three ways: recursion, mapping, and folding. Together, these three replace all loop patterns found in imperative languages.

Mapping transforms every element of a list, filtering selects the ones that match a condition, and folding reduces the list to a single value. Calculating totals, counting, and finding the maximum are all special cases of folding.

Recursion is the most fundamental tool, but writing it directly is usually unnecessary. Standard functions cover most scenarios, and code written with them is both concise and less prone to errors.

What you will learn

  • Mapping and filtering functions
  • Folding
  • Recursion and the base case
  • Built-in aggregation functions
  • Equivalents of loop patterns

Who is this for?

  • Developers looking for loops
  • Learners of recursion
  • Developers transitioning to functional patterns

When to use?

Mapping is used when an independent transformation is applied to every element, while filtering is used when selection is based on a condition.

Folding is the right tool when a single result is produced from a list; sum, product, and count are examples of this.

Common mistakes

Wrong

topla :: [Int] -> Int
topla (x:xs) = x + topla xs

Since the base case is not defined, no pattern remains to match when the list becomes empty, and the program stops with "Non-exhaustive patterns". Every recursive definition must have a stopping point; the definition is incomplete without writing the empty list case.

Correct

topla :: [Int] -> Int
topla [] = 0
topla (x:xs) = x + topla xs

Wrong

sayac :: Int -> Int
sayac n = sayac (n + 1)

Since there is no stopping condition, the recursion continues infinitely and the program stops responding. This is the equivalent of an infinite loop in imperative languages. In every recursion, the problem must shrink and approach a base case.

Correct

sayac :: Int -> Int
sayac n
  | n >= 10 = n
  | otherwise = sayac (n + 1)

Wrong

main = print (map (\x -> x > 2) [1, 2, 3, 4])

There is no error here, but the intent is likely expressed incorrectly: mapping turns every element into a boolean value, whereas filtering selects elements that meet a condition. If you want the ones that match the condition, a different function should be used.

Correct

main = print (filter (\x -> x > 2) [1, 2, 3, 4])

Frequently asked questions

What exactly does folding do?

It starts with an initial value and reduces it to a single result by combining every element in the list into this value. Addition, multiplication, finding the maximum, and even reversing a list can be written with folding; it is a very general tool.

Should I fold from the left or the right?

Right folding works with infinite lists and lazy combinations; for large finite lists, strict left folding is safe memory-wise. Since the default left folding is lazy, it can lead to stack overflow on large lists.

How to repeat with side effects?

There are special mapping functions to repeat operations like printing to the screen over a list. These discard the result and apply only the effect; this is how they differ from pure mapping functions.

Summary

There are no loops; recursion, mapping, and folding are used instead.

Every recursion must have a base case.

Mapping transforms, filtering selects, folding reduces.

If the base case is missing, a runtime error occurs.

Strict folding should be preferred for large lists.

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