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

Static Type Check

We also said that Haskell is a statically-typed language, which means that each expression type is known at compile-time instead of run-time. This makes Haskell very reliable β if we successfully compile our program, we can be sure that no type errors will occur. This benefit does come at a cost of increased compilation time, and doing any changes to our program requires us to re-compile it.

Another feature of Haskell is type inference, which means that Haskell can automatically interpret types of expressions. This can make our code more concise, but it is still encouraged to explicitly specify the types when we define functions, i.e. to write the functions** type signature**. For our previous function triple, we could add a type signature to it in our

`Practice.hs`

:1

triple :: Int -> Int -- function type signature

2

triple x = 3 * x -- function definition

Copied!

`triple :: Int -> Int`

reads as "triple is a function that takes in one argument of the type `Int`

and produces a result of a type `Int`

". Now that we have altered our module code, we need to reload the practice module in order to apply the changes in GHCi:1

*Practice> :r Practice -- :r stands for :reload

2

Ok, one module loaded.

3

β

4

*Practice> :t triple -- :t stands for :type

5

triple :: Int -> Int

6

β

7

*Practice> triple 3

8

9

Copied!

But what happens if we try to apply our

`triple`

function to a floating-point number?1

*Practice> triple 3.5

2

<interactive>:5:8: error:

3

β’ No instance for (Fractional Int) arising from the literal β3.5β

4

β’ In the first argument of βtripleβ, namely β3.5β

5

In the expression: triple 3.5

6

In an equation for βitβ: it = triple 3.5

Copied!

Polymorphism

We get a type error because we strictly defined the function as one that takes in an **overloaded type **specified with a **type class** - more on those on the next page), which supports both integer and floating-point numbers:

`Int`

as its only argument, but we passed in a floating-point number. So how can we make our function accept both `Int`

and `Float`

? To do that, we can use Haskell's polymorphism and specify a polymorphic type of `Num a`

(an 1

triple :: Num a => a -> a

2

triple x = 3 * x

3

*Practice> :r

4

*Practice> triple 3.5

5

10.5

Copied!

The line **polymorphism **where we can work with different types for the same function argument. Note that we use the variable named **type** **class**

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

now reads as "triple is a function that takes in one argument of type `a`

and gives a result of type `a`

where the type of `a`

is `Num`

". So here we see an example of `a`

as a placeholder for the `Num`

, but it could be any valid name starting with a lowercase character.Copy link

Contents