HPM Education - Haskell

Searchβ¦

Introduction

Types in Haskell

Defining Functions / Working with Functions

List Comprehensions

Higher-order Functions

Cutom Types

Interactive Programming

Functors, Applicatives and Monads

Polymorphic and Overloaded Types

We have already touched upon **polymorphic types** in our**function's type signature** to specify that it can accept both number types as arguments. The **type variable** and our function signature **class **

`triple`

function when we made it possible to triple both integers and floating-point numbers. We used`Num a`

in the `Num`

is a class constraint and `a`

is the `triple :: Num a => a -> a`

reads as "for any type `a`

that is an instance of the **Num**

, the function `triple`

has the type `a -> a`

".Any type that has a** class constraint** is called an **overloaded type**, and hence our**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

`triple`

function is an `head`

(return the first element of a list) and `tail`

(return 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

Copy link