History of Idiom 20 > diff from v37 to v38
Edit summary for version 38 by :
New Erlang implementation by user [elbrujohalcon]
New Erlang implementation by user [elbrujohalcon]
↷
Version 37
2016-03-21, 23:40:07
Version 38
2016-04-05, 04:13:29
Idiom #20 Return two values
Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.
Idiom #20 Return two values
Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.
Code
-spec search(T, [[T]]) -> {pos_integer(), pos_integer()}.
search(X, M) -> search(X, M, 1).
search(_, [], _) -> throw(notfound);
search(X, [R|Rs], RN) ->
case search_row(X, R) of
notfound -> search(X, Rs, RN+1);
CN -> {RN, CN}
end.
search_row(X, Row) -> search_row(X, Row, 1).
search_row(_, [], _) -> notfound;
search_row(X, [X|_], CN) -> CN;
search_row(X, [_|Elems], CN) -> search_row(X, Elems, CN+1).
Comments bubble