📘
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

Polymorphic and Overloaded Types

We have already touched upon polymorphic types in our triple function when we made it work with both integers and floating-point numbers. We used Num a in the function's type signature to specify that it can accept both number types as arguments. The Num is a class constraint and a is the type variable in our function signature. triple :: Num a => a -> a reads as "for any type a that is an instance of the class Num, the function triple has the type signature a -> a".

Any type that has a class constraint is called an overloaded type, and hence our triple function is an overloaded function. We can even also specify a type variable without the class constraint, in which case that type is completely polymorphic and any type can fill the arguments' place. For example, this is used in several list functions we used earlier, as their results do not depend on the types of elements that fill the lists. For example, the head (which returns the first element of a list) and tail (which returns the list excluding the first element) functions must work regardless of what type the elements in the list are. Therefore, their type signatures are:

head :: [a] -> a
-- a list of type a's returns a type a, whatever type a is for that list

tail :: [a] -> [a]
-- a list of type a's returns a type [a], whatever type a is for that list
PreviousStatic Type CheckNextData Structure Types

Last updated 2 years ago

Was this helpful?