๐Ÿ“˜
HPM Education - Haskell
  • Introduction to Haskell
  • Introduction
    • Functions
    • Functional Programming vs Imperative Programming
    • Installing Haskell
    • Haskell Modules
    • Loading Modules into GHCi
    • Expressions
    • Laziness
    • Immutability
  • Types in Haskell
    • Introduction
    • Basic Types
    • Static Type Check
    • Polymorphic and Overloaded Types
    • Data Structure Types
      • Lists
        • List Functions
      • Tuples
    • Function Types
      • Curried Functions
      • Partial Application
  • Defining Functions / Working with Functions
    • The Layout Rule
    • Local Definitions
    • The Infix Operator
    • Conditionals
      • If-then-else Statements
      • MultiWayIf
      • Guarded Equations
      • Case-of Statements
    • Pattern Matching
      • Tuple Patterns
      • List Patterns
    • Lambda functions
    • Function Operators
  • List Comprehensions
    • List Comprehensions
  • Higher-order Functions
    • Introduction
    • The map Function
    • The filter Function
  • Recursion
    • Introduction
    • 4 Steps to Defining Recursive Functions
    • Recursion Practice
    • Folds
      • Fold Right (foldr)
      • Fold Left (foldl)
  • Cutom Types
    • Declaring Types
      • Type Synonyms
      • Data Declarations
      • Newtype declarations
  • Type Classes
    • Introduction
    • Basic Classes
      • Eq โ€“ Equality Types
      • Ord โ€“ ordered types
      • Show โ€“ Showable Types
      • Read โ€“ readable types
      • Num โ€“ Numeric Types
      • Integral โ€“ Integral Types
      • Fractional โ€“ Fractional Types
      • Enum โ€“ Enumeration Types
    • Derived Instances
    • Exercise โ€“ Making a Card Deck Type
  • Interactive Programming
    • Introduction
    • Input / Output Actions
    • Sequencing Actions
    • Exercise - Numbers Guessing Game
  • Functors, Applicatives and Monads
    • Introduction
    • Functors
    • Applicative Functors
    • Monads
      • Maybe Monad
      • List Monad
      • Monad Laws
  • References / Further Reading
Powered by GitBook
On this page

Was this helpful?

  1. Types in Haskell
  2. Data Structure Types

Tuples

Tuples, unlike lists, can contain elements of different types. The elements of a tuple are enclosed in round parentheses and separated by commas:

("Cardano", True) :: (String, Bool)

The above is an example of a tuple pair that is comprised of two elements, in this case, a string and a boolean. Notice that the tuple type is dependent on its elements โ€“ both the number of elements and their individual types. Unlike with lists, where we have one clear type for any single list regardless of its length, tuples must be finite and the type of each element must be known in order for the Haskell type system to able to work properly. The length of a tuple is sometimes referred to as its arity.

[1] :: [Int] -- The type of the list is the same...
[1, 2, 3] :: [Int] -- ...regardless of its length.

(1, 2) :: (Int, Int) -- The type of the tuple changes...
(1, 2, False) :: (Int, Int, Bool) -- ...with its length (and element types).

Tuples are most commonly used as key-value pairs โ€“ a data structure for storing and retrieving data. That is why I said pairs are the most common of tuples โ€“ for example, a nameโ€“address pair or a dictionary word-definition pair.

PreviousList FunctionsNextFunction Types

Last updated 2 years ago

Was this helpful?