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›Data Types

Data Types

Int, Double, String, Bool; concatenation (++) and text↔number conversion (show/read).

Simple enough for a 5-year-old to understand

Concatenation and show

Strings are concatenated with ++. show converts a value to text.

Example code
1
main = do
2
let name = "Alex"
3
let age = 36
4
putStrLn (name ++ " " ++ show age ++ " years old")
Alex is 36 years old
Output / InputAlex is 36 years old
Output / Input
💡 Hint: Convert the number to text using show before adding it to the text.

About this topic

Haskell is a powerful and strongly statically typed language: the type of every value is known at compile time, and types do not convert implicitly. In contrast, its type inference is very advanced, so you often do not need to write out the types.

The basic types are Int, Integer, Double, Char, and Bool. Int is a fixed-width integer and can overflow; Integer, on the other hand, grows as much as memory allows. This distinction is unique to Haskell and is important when working with large numbers.

Haskell's type classes specify which operations a type supports. Numeric operations belong to the Num class, and division belongs to the Fractional class. Therefore, you cannot use the normal division operator with integers — and the error message says precisely that.

What you will learn

  • Basic types
  • Those trying to understand the difference between Int and Integer
  • Division operators
  • The role of type classes
  • Conversion between types

Who is this for?

  • Those trying to resolve type errors
  • Those getting an error during division
  • Those seeing type classes for the first time

When to use?

In tasks requiring large numbers like cryptography and combinatorics, using Integer eliminates the risk of overflow.

Double is sufficient for decimal calculations; in monetary calculations, storing integers as cents to avoid rounding errors is a common solution.

Common mistakes

Wrong

main = print (5 / 2 :: Int)

The division operator belongs to the Fractional class, and Int is not a member of this class; the error "No instance for (Fractional Int)" is received. There is a separate function for integer division. The error message states exactly which class is missing, which makes learning to read Haskell error messages worthwhile.

Correct

main = print (div 5 2)

Wrong

topla :: Int -> Int -> Int
topla a b = a + b
main = print (topla 2 3.5)

A decimal number was provided while the function expected an Int; the error "No instance for (Fractional Int)" is received. Haskell does not convert numbers implicitly. If you are going to use a decimal value, you need to make the signature Double.

Correct

topla :: Double -> Double -> Double
topla a b = a + b
main = print (topla 2 3.5)

Wrong

main = do
  let n = 5 :: Int
  let x = 2.5 :: Double
  print (n + x)

Numbers of different types cannot be added directly; both operands must be of the same type. You must write the conversion explicitly. This conversion, which is done silently in many languages, is made visible in Haskell.

Correct

main = do
  let n = 5 :: Int
  let x = 2.5 :: Double
  print (fromIntegral n + x)

Frequently asked questions

Which one should I choose between Int and Integer?

If you are sure the values will remain limited, Int is faster. In rapidly growing calculations like factorials, you must use Integer; otherwise, you will experience silent overflow and the result will be wrong.

How do I convert a number to text?

The show function is used and works with almost every type. In the opposite direction, the read function converts text to a value, but you may need to specify the target type; if you don't, the compiler won't know which type to convert it to.

What exactly is a type class?

It is a contract that defines which operations a type supports. Equality comparison belongs to the Eq class, ordering to the Ord class, and representation to the Show class. You can write generic code that works with multiple types by writing a class constraint instead of a type in function signatures.

Summary

Types do not convert implicitly.

Int has a fixed width, while Integer grows without limit.

Integer division is a separate function.

Different number types require explicit conversion.

Type classes determine the supported operations.

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