what does this symbol | mean in Haskelll
Asked Answered
H

4

6

Someone can explain this coding to me.

[ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]

I don't understand the meaning of this | symbol in haskell

Hanoverian answered 22/1, 2014 at 9:2 Comment(0)
T
4

symbol '|' has the same meaning as symbol '|' in math (set theory). You just should read it like 'such that'. In math the symbol '|' sometimes is replaced by ':'.

symbol '<-' is read as 'is drawn from'.

And the expression x <- [2,5,10] is called a generator. A list comprehension can have more than one generator, with successive generators being separated by commas.

List comprehensions can also use logical expressions called guards to filter the values produced by earlier generators. If a guard is True, then the current values are retained, and, if it is False, then they are discarded. For example, the comprehension [x | x <- [1..10], even x] produces the list [2,4,6,8,10] of all even numbers from list [1..10].

Hope it would help you to understand the meaning of symbol '|' and '<-' in list comprehensions.

Tennison answered 22/1, 2014 at 14:0 Comment(0)
B
12

You should read it as "where" or "such that" -

-- x * y where x is from [2,5,10] and y is from [8,10,11] and x * y > 50
[  x * y   |   x    <-   [2,5,10],    y    <-   [8,10,11],    x * y > 50]

or, alternatively, if you're familiar with Python and its list comprehensions, you might read it as "for"

-- [x * y for x in [2,5,10] for y in [8,10,11] if x * y > 50]
   [x * y  |  x <- [2,5,10],    y <- [8,10,11],   x * y > 50]
Barrios answered 22/1, 2014 at 9:5 Comment(2)
@Koushik The same symbol is used in function definitions to introduce a guard, but that's not what they're being used for here.Barrios
The overall construct, including the | token, is known as list comprehension.Fustigate
T
4

symbol '|' has the same meaning as symbol '|' in math (set theory). You just should read it like 'such that'. In math the symbol '|' sometimes is replaced by ':'.

symbol '<-' is read as 'is drawn from'.

And the expression x <- [2,5,10] is called a generator. A list comprehension can have more than one generator, with successive generators being separated by commas.

List comprehensions can also use logical expressions called guards to filter the values produced by earlier generators. If a guard is True, then the current values are retained, and, if it is False, then they are discarded. For example, the comprehension [x | x <- [1..10], even x] produces the list [2,4,6,8,10] of all even numbers from list [1..10].

Hope it would help you to understand the meaning of symbol '|' and '<-' in list comprehensions.

Tennison answered 22/1, 2014 at 14:0 Comment(0)
P
3

A translation to English would be something like

A list whose elements are of the form x*y, such that x is an element of [2,5,10], y is an element of [8,10,11], and x*y is greater than 50.

The | symbol is here part of the syntax for list comprehensions; it's not an operator or anything else that has independent meaning, it simply serves to separate the expression for the elements of the list being defined (the x*y* part in this case) from the generators and filters (the x <- [2,5,10], y <- [8,10,11], x*y > 50 part). In the translation to English, I rendered the | symbol as "such that"; "where" is also common.

The syntax for writing list comprehensions is inspired by how set comprehensions are written in mathematics; in the examples on that page you can clearly see a vertical bar used to separate the form of set elements from the conditions on the elements.

Primogeniture answered 22/1, 2014 at 11:38 Comment(0)
O
1

I would prefer to think the | here as under these condition:

Overuse answered 22/1, 2014 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.