Headline

A language construct for list processing

Illustration

Mapping over a list

Suppose you want to map over a list of numbers to increment each number. The following list comprehension implements this requirement:

> [ x+1 | x <- [1,2,3,4,5] ]
[2,3,4,5,6]

The expression after the bar "|" is the generator for elements x. The expression before the bar computes the element of the resulting list from the generated elements. The Map function actually models such element-wise mapping. Thus, the list comprehension could also be expressed as follows:

> map (\x -> x + 1) [1,2,3,4,5]
[2,3,4,5,6]

Filtering a list

Suppose you want to filter a list of numbers to only keep odd numbers. The following list comprehension implements this requirement:

> [ x | x <- [1,2,3,4,5], odd x ]
[1,3,5]

There are two expressions after the bar. The first one is a generator for elements x, the second is a guard to impose a condition of generated elements. The Filter function actually models filtering according to a guard (i.e., a predicate). Thus, the list comprehension could also be expressed as follows:

> filter odd [1,2,3,4,5]
[1,3,5]

Multiple generators and guards

List comprehensions can use multiple generators and guards, the scope of the generators extends to the subsequent generators and guards. For instance, we may collect all combinations of pairs of elements drawn from two separate lists as follows:

> [ (x,y) | x <- [1,2,3], y <- ['a','b'] ]
[(1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b')]

Let us compute the sum of all possible pairs with elements drawn from one list, where a guard is applied to reject pairs of identical elements:

> let sample = [1,2,3,4,5]
e> [ x + y | x <- sample, y <- sample, x /= y ]
[3,4,5,6,3,5,6,7,4,5,7,8,5,6,7,9,6,7,8,9]


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    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.