如果我写简单:
[X || X <- [1,2,3,4,5,6,7,8,9,10]].
它返回:
[1,2,3,4,5,6,7,8,9,10]
如果我写:
[{a,b} || {a,b} <- [{1,2},{2,3}]].
它返回
[]
简单问题 - 为什么?
答案 0 :(得分:3)
生成器 {a,b}&lt; - [{1,2},{2,3}] 同时是一个过滤器。因此,当元素与模式不匹配时,它就会跳过。
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V8.2 (abort with ^G)
1> [X || X <- [1,2,3,4,5,6,7,8,9,10]].
[1,2,3,4,5,6,7,8,9,10]
2> [{a,b} || {a,b} <- [{1,2},{2,3}]].
[]
3> [{a,b} || {a,b} <- [{1,2},{2,3},{a,b}]].
[{a,b}]
4>
了解有关列表推导的更多信息
7> Weather = [{toronto, rain}, {montreal, storms}, {london, fog},{paris, sun}, {boston, fog}, {vancouver, snow}].
[{toronto,rain},
{montreal,storms},
{london,fog},
{paris,sun},
{boston,fog},
{vancouver,snow}]
8> FoggyPlaces = [X || {X, fog} <- Weather].
[london,boston]
9> FoggyPlaces = [X || {X, fog1} <- Weather].
** exception error: no match of right hand side value []
10> [X || {X, fog1} <- Weather].
[]
11>
答案 1 :(得分:1)
如果我写:
[{A,B} || {A,B} <- [{1,2},{2,3}]].
它返回
[{1,2},{2,3}]