开发者

Erlang list comprehension, traversing two lists and excluding values

开发者 https://www.devze.com 2022-12-22 16:02 出处:网络
I need to generate a set of coordinates in Erlang. Given one coordinate, say (x,y) I need to generate (x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1). Basically

I need to generate a set of coordinates in Erlang. Given one coordinate, say (x,y) I need to generate (x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1). Basically all surrounding coordinates EXCEPT the middle coordinate (x,y). To generate all the nine coordinates, I do this开发者_StackOverflow中文版 currently:

[{X,Y} || X<-lists:seq(X-1,X+1), Y<-lists:seq(Y-1,Y+1)]

But this generates all the values, including (X,Y). How do I exclude (X,Y) from the list using filters in the list comprehension?


[{X,Y} || X <- lists:seq(X0-1,X0+1),
          Y <- lists:seq(Y0-1,Y0+1), {X,Y} =/= {X0,Y0}].


I think distinguish between parameters and generated values will help a little:

[{Xc,Yc} || Xc<-lists:seq(X-1,X+1), Yc<-lists:seq(Y-1,Y+1), Xc=/=X orelse Yc=/=Y]

or else

[{Xc,Yc} || Xc<-lists:seq(X-1,X+1), Yc<-lists:seq(Y-1,Y+1)] -- [{X,Y}]


Adding -- [{X,Y}] would probably be the easiest thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号