Core Types and Literals
Binding Values and Type Signatures
Welcome to the second unit of our course! In the previous lesson, we learned how to write our first Haskell program by creating a main block and using putStrLn to display a message on the screen.
Any useful program needs to work with data. In Haskell, we do this by binding a piece of data to a name using the equals sign (=).
Here, we are storing the number 42 under the name count. However, Haskell is a strictly typed language, which means it likes to know exactly what kind of data a name holds. We provide this information using a type signature.
The double colon (::) can be read as "has the type of." In this example, we are telling Haskell that count has the type of Int (short for integer), and then we assign it the value 42. Providing these type signatures makes our code safer and much easier to read.
Number Types: Int, Integer, and Double
Haskell has built-in types for different kinds of numbers. Let us start by looking at the standard whole number, the Int.
An Int is used for everyday whole numbers, like counting items or indexing positions. However, Int has a maximum size limit depending on the computer running the code. If you need to store an incredibly large whole number, Haskell provides the Integer type.
Unlike Int, an Integer is unbounded, meaning it can hold a number as large as your computer's memory can handle.
Finally, when you are dealing with fractional numbers or measurements, you will use the Double type.
The Double type represents numbers with decimal points. By specifying Double, Haskell knows to expect a fractional value rather than a whole number.
A quick note on money: although a Double can hold a value like 19.95, real-world money is usually not stored as a Double. Because Double uses binary floating-point, it can introduce tiny rounding errors over time. Production code typically represents currency with integer cents, a fixed-point type, or a dedicated decimal library. We use 19.95 here purely to illustrate a decimal value.
Text and Logic Types: Char, String, and Bool
