Learn/Haskell/2. Values and names
Lesson 2 of 8
Sign in

Haskell, lesson 2

Values and names

In most languages a variable is a box whose contents change. Haskell is different: a name is bound to a value once, and it means that value forever. There is no assignment that overwrites it. This sounds limiting, but it means a name can never surprise you by changing behind your back.

These are top-level definitions. The line with :: is the type signature; the line with = gives the value. Writing level = 8 a second time in the same file is an error, not an update.

Basic types

TypeHoldsExample
Intwhole numbers (64-bit)42

2 exercises to try what you just read.

Exercise 1

Character card

Press Ctrl Enter or Check to test your code.

Exercise 2

Piggy bank

Press Ctrl Enter or Check to test your code.

Integer
whole numbers of any size
2 ^ 100
Doublenumbers with a decimal point3.5
BoolTrue or False (capital letters)True
Charone character, in single quotes'A'
Stringtext, in double quotes"tea"

A String is really a list of Char, which is why many list functions work on text too.

Haskell never converts between types silently. You cannot add an Int to a Double directly; you convert first with fromIntegral. We will use that in the next lesson.

Naming

Names of values and functions start with a lowercase letter and usually use camelCase: maxHealth, totalPrice. Names that start with an uppercase letter are reserved for types and constructors, like Int and True. A trailing apostrophe is allowed and common for a "next version" of something: score'.

Local names: let and where

Inside main you can introduce local names with let:

Several bindings in one let must line up under each other. Outside a do block you can attach local names to a definition with where, which reads like a footnote:

Building text: show and ++

++ joins two strings. It only works on strings, so a number must first be turned into text with show:

Don't show a string you want to display as-is, because show "tea" includes the quotes.

"Changing" a value

When you need an updated value, you make a new name for it:

start is still 20. This style, building new values from old ones instead of changing them, is at the heart of functional programming.

Further reading

  • A Gentle Introduction to Haskell: Values, Types, and Other Goodies
  • Haskell 2010 Report: Expressions (let, where)
  • Prelude: show

The starter defines four values for a game character. Print them on one line in exactly this format:

Use ++ and show. The program reads no input.

Savings start at 20. Instead of changing one variable, create a new name for each step:

  1. afterDeposit adds a deposit of 15.
  2. doubled doubles that.
  3. afterSnack spends 8.

Print every step:

playerName :: String
playerName = "Rowan"

level :: Int
level = 7
main :: IO ()
main = do
  let cups = 3
      item = "tea"
  putStrLn (show cups ++ " cups of " ++ item)
area :: Double
area = width * height
  where
    width = 4.0
    height = 2.5
main :: IO ()
main = do
  let score = 12 :: Int
  putStrLn ("Score: " ++ show score)     -- Score: 12
  putStrLn ("Mage? " ++ show True)       -- Mage? True
let start = 20
    later = start + 5
Rowan | level 7 | 42.5 HP | mage: True
Start: 20
After deposit: 35
Doubled: 70
After snack: 62