Erlang: intersection of region N by N

I am looking to cross the NxN area, given the starting points X, Y and the size of the square to move. For example. given X = 10, Y = 12, size = 2 - I want to generate 10.10; 10.11; 11.10 and 11.11.

I came up with this, but it looks like it goes on endlessly:

traverse({X,Y,Xend,Yend}) ->

    % print X,Y values here....     

    case (X == Xend-1) andalso (Y == Yend-1)  of
        true -> 
           ok;
        _->         
           case (Y < Yend-1) of
              true ->
            traverse({X,Y+1,Xend,Yend});
              _->
            traverse({X+1,Y,Xend,Yend})
           end
    end.

I called the above function from another function using:

Size = 3,
traverse({10,20,10+Size,20+Size}).

What am I doing wrong? I'm actually new to the functional paradigm, and I tried to implement this in C using "return" instead of "ok" above, and it worked, but I guess I don't think "functionally" enough!

+3
source share
3 answers
traverse(X0, Y0, S) ->
    [
     io:format("coords: ~p~n", [{X,Y}])
    || X <- lists:seq(X0, X0 + S - 1),
       Y <- lists:seq(Y0, Y0 + S - 1)
    ],
    ok

in the shell:

1> traverse(10, 12, 2).
coords: {10,12}
coords: {10,13}
coords: {11,12}
coords: {11,13}
ok
+3
source

:

traverse({X,Y,Xend,Yend}) ->
 dotraverse(X, Y, Xend, Yend, _StartX=X).

dotraverse({Xend,Yend,Xend,Yend}, SX) ->
 ok;

dotraverse({X,Y,Xend,Yend}, SX) when X<Xend ->
  %print
  dotraverse({X+1, Y, Xend, Yend}, SX);

dotraverse({Xend,Y,Xend,Yend}) ->
 dotraverse(SX,Y+1, Xend, Yend}, SX).

: , .

+2

, - . , , , .

C, , , .

"" , " ", , C. Zed , .

- , , . , , , .

, " OTP" erlang.org. -, .

+2
source

Source: https://habr.com/ru/post/1725182/


All Articles