erlang 什么|和||在爱尔兰语中是什么意思?

k2fxgqgv  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(176)

我已经检查了erlang网站运营商等,但我找不到什么||和|手段。
我在什么地方读到||意思是“这样的”但什么只是一个|“意思是?

sxpgvts3

sxpgvts31#

| is the "cons" operator: It puts an element in front of a list:

1> [1 | [2,3]].
[1,2,3]
2> [[1, 2] | [3,4,5]].
[[1,2],3,4,5]

|| is used in list-comprehensions. In its simplest form, it can be used as a short-hand for map :

3> [2 * X || X <- [1,2,3]].
[2,4,6]

But it becomes much more handy when you want to write multiple generators, creating the cartesian product:

4> [{X, Y} || X <- [1,2,3], Y <- [4, 5, 6]].
[{1,4},{1,5},{1,6},{2,4},{2,5},{2,6},{3,4},{3,5},{3,6}]

You can also do filter along the way. Compare:

5> [X+Y || X <- [1,2,3], Y <- [4,5,6]].
[5,6,7,6,7,8,7,8,9]

to:

6> [X+Y || X <- [1,2,3], Y <- [4,5,6], X+Y > 6].
[7,7,8,7,8,9]

The | operator is essential, in the sense that it is the canonical way how you construct a new list out of an existing head element and a tail of the list. The same notation also works in pattern matching, i.e., it is also how you deconstruct a list.
On the other hand, list comprehensions are mostly syntactic sugar: They can be written using regular function applications and hence is not fundamental to the language. But they can significantly improve readability by getting rid of syntactic noise, mimicking set-comprehension like mathematical notation directly within the language.

相关问题