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 (=).

Haskell
count = 42

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.

Haskell
count :: Int
count = 42

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.

Haskell
count :: Int
count = 42

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.

Haskell
bigNumber :: Integer
bigNumber = 9000000000000000000

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.

Haskell
price :: Double
price = 19.95

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

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal