Types and type signatures

Contents
Basic types
Lists, tuples, functions
Read a signature
Int or Integer

Basic types

A type names a set of values. The ones you meet on day one are small and precise:

Int -- 64-bit signed integer on modern platforms Integer -- arbitrary precision integer Double -- 64-bit floating point Bool -- True or False Char -- one Unicode code point String -- [Char], a list of characters

Type names start with a capital letter, values with a lower case letter. That convention is enforced by the parser, not by taste.

Lists, tuples, functions

Compound types are written with brackets and arrows. A list holds any number of values of one type; a tuple holds a fixed number of values of possibly different types:

[Int] -- list of integers [(String, Int)] -- list of pairs (Int, Bool, Char) -- a triple Int -> Int -- function from Int to Int Int -> Int -> Int -- two arguments

The last line has no separate syntax for several arguments: it is a function that takes an Int and returns a function. Lesson 8 shows why that distinction pays off.

Read a signature

Everything left of the last arrow is an argument, everything right of it is the result. The name before the double colon is the value being described:

repeatN :: Int -> String -> String repeatN n s = concat (replicate n s) -- repeatN 3 "ab" == "ababab"

Signatures are optional, because GHC infers them, but a top-level function without one is a gift to future confusion. Write the signature first and let the compiler tell you when the body disagrees.

Int or Integer

Int wraps a machine word and can overflow silently. Integer grows as needed and is the safe default for arithmetic that may exceed 64 bits:

big :: Integer big = product [1..30] -- 265252859812191058636308480000000

The same expression typed as Int still fits here, but at 21 factorial it does not. Choose Integer when the size is unknown and Int when it is a counter or an index.

Next: Functions and application

Article author: Arthur Isaev

Related articles
Haskell
Haskell course: zero to hero in 50 lessons
What is Haskell and why it matters
Install the toolchain with GHCup
GHCi: the interactive shell
Your first program
Values with let and where
Types and type signatures
Functions and application
Currying and partial application
Operators, sections, and fixity
if, guards, and pattern guards
case expressions
Pattern matching
Tuples
Lists
Ranges and enumeration
List comprehensions
Recursion replaces loops
map, filter, and foldr
Lambdas and composition
Type classes
Parametric polymorphism
Maybe and null safety
Either and recoverable errors
data: product types
data: sum types
Records
newtype and type synonyms
Recursive data: binary trees
Functor
Applicative
Monad
do notation
IO and purity
Files and IO
Modules and imports
Cabal projects
Dependencies
Testing with HUnit
Property testing with QuickCheck
Laziness and infinite lists
Strictness and space leaks
Folds
Monoids
Traversable
Monad transformers
Errors and exceptions
Concurrency
Parsing with Megaparsec
Capstone: a todo CLI
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: