# Map function

A higher-order function to apply an argument function to all elements of a list

## Illustration

Let's map over a given list of numbers to increment them. In Language:Haskell, we would use the map function as follows:

```>  map (+1) [1,2,3,4,5]
[2,3,4,5,6]
```

That is, map is applied to a function and a list; map returns a list of the same length as the input list which each element "transformed" by the argument function. The higher-order function map can be defined as follows:

```-- Define map via pattern matching
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
```

For what it matters, the map function could also be defined in terms of the fold function, thereby reusing a recursion scheme as opposed to the explicitly recursive definition above.

```-- Define map via foldr
map' :: (a -> b) -> [a] -> [b]
map' f = foldr ((:) . f) []
```

Further, we can also define the map function in terms of list comprehension; in this manner we hint at the meaning of list comprehensions because, in fact, list comprehensions correspond to syntactic sugar whose elimination also involves the Map function.

```-- Define map via list comprehension
map'' :: (a -> b) -> [a] -> [b]
map'' f xs = [ f x | x <- xs ]
```

Conceptually, map functions are not limited to lists; they make sense for algebraic data types in general. List-like maps can be generalized to functors.

Ralf Lämmel edited this article at Wed, 17 Jun 2020 12:40:57 +0200

## User contributions

This user never has never made submissions.

## Syntax for editing wiki

For you are available next options:

will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from @worker.

## Syntax for editing wiki

For you are available next options:

will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from @worker.