lundi 23 février 2015

Erlang : nested cases

I'm very new to Erlang. I tried to find out if a list index is out of bounds (before trying it) so i wanted to do an if clause with something like



if lists:flatlength(A) < DestinationIndex ....


I discovered that those function results cannot be used in if guards so i used case instead. This results in a nested case statement



case Destination < 1 of
true -> {ok,NumberOfJumps+1};
false ->
case lists:flatlength(A) < Destination of
true ->
doSomething;
false ->
case lists:member(Destination,VisitedIndices) of
true -> doSomething;
false ->
doSomethingElse
end
end
end.


I found this bad in terms of readability and code style. Is this how you do things like that in erlang or is there a more elegant way to do this?


Thanks in advance


Aucun commentaire:

Enregistrer un commentaire