📘
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

Lists

Lists are sequences of elements of the same type and are a key component of Haskell. This means that a list can only hold elements of the same type, e.g. a list of Ints as we used in our example function - sum. To create lists in Haskell, we put their elements in square brackets and separate them with commas:

[False, False, True] :: [Bool] -- a list of booleans
[1, 3, 5] :: [Int] -- a list of integers
['a', 'b', 'c'] :: [Char] -- a list of characters

Lists can also contain other lists:

[[1, 2, 3], [4, 5, 6]] :: [[Int]] -- a list of lists of integers

But remember - lists are sequences of elements of the same type, so a list of lists must not contain lists of different types. For example, we cannot combine a list of Ints and a list of Chars into a single list of lists:

ghci> x = [[1, 2, 3], ['a', 'b', 'c']]

<interactive>:2:7: error:
    • No instance for (Num Char) arising from the literal ‘1’
    • In the expression: 1
      In the expression: [1, 2, 3]
      In the expression: [[1, 2, 3], ['a', 'b', 'c']]

Lists can also be empty ([]) and a special case called a singleton list is ([[]]), which is a list with its single element being an empty list. Lists in Haskell can also be infinite.

PreviousData Structure TypesNextList Functions

Last updated 2 years ago

Was this helpful?