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
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
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.
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]
|
token, is known as list comprehension. –
Fustigate 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.
A translation to English would be something like
A list whose elements are of the form
x*y
, such thatx
is an element of[2,5,10]
,y
is an element of[8,10,11]
, andx*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.
I would prefer to think the |
here as under these condition:
© 2022 - 2024 — McMap. All rights reserved.