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

Function Types

We know that **functions **in Haskell are **expressions **and each expression must have a type, so what types can a function have? Functions take in some arguments and create a result of some type which can be the same as some (or all) of its arguments or a different one. We have already touched upon the subject of function types in our example function

`triple`

:1

triple :: Int -> Int

2

triple x = 3 * x

Copied!

In this case, the type of our function triple is

`Int -> Int`

β it takes an`Int`

as its only argument and returns an`Int`

as the result. As functions are expressions, we can use them as any other type of data, for example, we can create a list of functions:1

ghci> funList = [(+), (*)] -- (+) and (*) are functions for addition and multiplication

2

ghci> :t funList

3

Num a => [a -> a -> a]

Copied!

We can see that **function type** that takes two arguments of type

`funList`

is a list of a certain type β specifically, a `Num`

and returns a `Num`

type as its result.Last modified 8mo ago

Copy link