3

假设我有一个包含天气的列表:

1> Weather = [{toronto, rain}, {montreal, storms}, {london, fog}, 
    {paris, sun}, {boston, fog}, {vancouver, snow}].

为了得到有雾的地方,我可以这样做:

2> FoggyPlaces = [X || {X, fog} <- Weather].
[london,boston]

现在我想检索既有雾又有雪的地方。我试过这个,但它只检索下雪的地方,

3> FoggyAndSnowyPlaces = [X || {X, fog} <- Weather, {X,snow} <- Weather].
[vancouver,vancouver]

我期待的地方[london,boston,vancouver]

如何包含多个过滤器?

4

1 回答 1

13
FoggyAndSnowyPlaces = [X || {X, Y} <- Weather, (Y == fog) or (Y == snow)].

You are confusing generators (Pattern <- List) and filters (boolean conditions). Multiple generators work like nested loops in other languages, so in your 3> you get vancouver twice because the first generator produces two values.

于 2013-01-14T15:37:55.597 回答