次のアプローチを試してみます。口座番号を数字だけにする必要があるのか、文字だけにする必要があるのかわかりませんでしたので、数字だけにしてほしいと勝手に決めました。
コメントに合わせて編集 *
check_num_compute(NumeroCompte) when length(NumeroCompte) == 9->
case io_lib:printable_unicode_list(NumeroCompte) of
true -> validate_contents(NumeroCompte);
false -> {error, not_string_of_numbers}
end;
check_num_compute(NumeroCompte) when is_list(NumeroCompte) ->
{error, wrong_length};
check_num_compute(_) ->
{error, not_string}.
validate_contents(NumeroCompte)->
AcceptFn = fun(C)->C >= $0 andalso C =< $9 end,
case lists:dropwhile(AcceptFn, NumeroCompte) of
[] -> true;
_ -> {error, not_all_numbers}
end.
19> t:check_num_compute([1,2,3,4,5,6,7,8,9]).
{error,not_string_of_numbers}
20> t:check_num_compute("123456789").
true
21> t:check_num_compute([1,2,3,4,5,6,7,8,9]).
{error,not_string_of_numbers}
23> t:check_num_compute("12345678f").
{error,not_all_numbers}
25> t:check_num_compute([]).
{error,wrong_length}
アカウント番号を文字だけにしたい場合は、validate_contents/1 を変更するだけで十分です。
また、lists:dropwhile/2 アプローチよりも次の方法を好む場合があります。
validate_contents([]) ->
true;
validate_contents([C|Cs]) when C >= $0, C =< $9 ->
validate_contents(Cs);
validate_contents(_) ->
{error, bad_arg}.