Haskell Basic Types
A type is a label that says what kind of value something is. Haskell uses types to check that values are used in ways that make sense.
Int holds a whole number
The annotation labels count as an Int. The binding below it gives the value.
count :: Int
count = 42
main :: IO ()
main = print countDouble holds a decimal number
The annotation labels price as a Double. The value has a decimal part.
price :: Double
price = 79.5
main :: IO ()
main = print priceBool holds True or False
The annotation labels ready as a Bool. Its value here is True.
ready :: Bool
ready = True
main :: IO ()
main = print readyChar holds one character
The annotation labels initial as a Char. A Char value sits in single quotes.
initial :: Char
initial = 'A'
main :: IO ()
main = print initialExample
name :: String
name = "Ana"
main :: IO ()
main = putStrLn nameKey points
- Int: a fixed-size whole number type.
- Integer: a whole number type that can hold numbers of any size.
- Double: a decimal number type, such as 3.5.
- Bool: a type with only two values, True and False.
- Char: a type for a single character, written in single quotes.
- String: a type for text, written in double quotes.
- Annotation: a type label written with ::, which reads as has type.
In this example, the annotation labels name as a String. The binding gives it a text value, and putStrLn prints that text.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Haskell Playground
Exercise
Which type fits the value 3.5?