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›Variables (let)

Variables (let)

Binding name with let; values are immutable in Haskell.

Simple enough for a 5-year-old to understand

binding with let

In a do block, you give a name to a value using let. Values are immutable — they are bound only once.

Example code
1
main = do
2
let name = "Alex"
Assigns "Alex" to name.
VariableAssigns "Alex" to name.
3
let age = 36
4
putStrLn name
Alex
Output / InputAlex
5
print age
36
Output / Input36
VariableOutput / Input
💡 Hint: In Haskell, a name CANNOT be reassigned a new value later; values are immutable.

About this topic

The word "variable" is misleading in Haskell because they are immutable. You bind a value to a name, and that binding remains constant throughout its scope. This is called a definition, not an assignment.

At first glance, this restriction might seem like a problem, but it guarantees one thing: when you see the value of a name, you know that name never changes anywhere else. That is why it is easy to understand the behavior of the code locally.

Local definitions are written in two forms. let defines temporary names inside an expression; where allows you to put helper definitions under a function definition. The choice between the two is mostly a matter of readability preference.

What you will learn

  • Value binding
  • The meaning of immutability
  • Using let and where
  • The consequences of shadowing
  • Writing a type signature

Who is this for?

  • Beginners to functional programming
  • Learners of the concept of immutability
  • Developers looking for a loop counter

When to use?

Local definitions are used to break down a complex expression; this both improves readability and prevents the same calculation from being repeated.

If a helper only makes sense in a single function, it should be written under that function using where; moving it to the top level pollutes the namespace.

Common mistakes

Wrong

main = do
  let x = 5
  let x = x + 1
  print x

The second definition is not an assignment, but a new definition that refers to itself; the program enters an infinite loop and stops responding. You cannot "update" a name in Haskell; you must give a new name. This is the behavior that those coming from imperative languages find most surprising.

Correct

main = do
  let x = 5
  let y = x + 1
  print y

Wrong

main = do
  let toplam = 0
  mapM_ (\n -> toplam = toplam + n) [1, 2, 3]
  print toplam

There is no assignment statement in Haskell; this code will result in a parsing error. Instead of a loop and a counter to accumulate a sum, a fold function is used. This is one of the clearest examples of the language's way of thinking.

Correct

main = do
  let toplam = sum [1, 2, 3]
  print toplam

Wrong

alan :: Double -> Double
alan r = pi * r * r
  where pi = 3.14

There is no error here, but it is an undesirable situation: the local definition shadows the more precise value in the standard library and the compiler issues a warning. Shadowing is allowed in Haskell, but redefining a known name misleads the reader.

Correct

alan :: Double -> Double
alan r = pi * r * r

Frequently asked questions

How is a counter maintained if the value does not change?

It is not; instead, a new value is passed to each call in recursion, or a fold function is used. Rather than keeping the counter outside, you calculate and pass the next state at each step. Once you get used to it, this approach is safer because intermediate states are not shared.

Should I choose between let and where?

where can only be used under function definitions and is visible across all guard branches; let works inside any expression. If you need the same helper in multiple branches, where is the right choice.

Why does shadowing produce a warning?

Because having two different meanings for the same name is a source of errors: it becomes ambiguous to the reader which definition is active. Such situations are reported when compiler warnings are enabled, and changing the name is usually the correct solution.

Summary

Names are bound to values, values do not change.

A definition that refers to itself produces an infinite loop.

There is no assignment statement in the language.

let defines within an expression, where defines under a function.

Shadowing is allowed but not recommended.

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