1

私はアーランから始めました

今のところ、アカウント番号をパラメーターとして受け取る関数があります

そして、この関数で私はテストを行います:

この数値が空かどうかをテストします 文字数が 9 に等しいかどうかをテストします

これらの文字が数字か文字かをテストします

この関数の構造は次のとおりです。

  checkNumCompte(numeroCompte) ->
if numeroCompte==null
......

サブ関数を開発する必要があると思います.1つ目は文字数を確認することです.2つ目は文字の形式を確認することです.

よろしくお願いします

アレン

4

3 に答える 3

0

次のアプローチを試してみます。口座番号を数字だけにする必要があるのか​​、文字だけにする必要があるのか​​わかりませんでしたので、数字だけにしてほしいと勝手に決めました。

コメントに合わせて編集 *

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}.
于 2012-12-12T21:51:04.370 に答える
0

erlang での使用法は、try を使用して変数をパターンと照合し、ガードを使用することです。たとえば、あなたが与えた3つのケースでは、次のように書くことができます:

checkNumCompte(C=[_,_,_,_,_,_,_,_,_]) -> % check that C is a list of 9 elements
                                         % you could imagine to test that 2 charaters are equal using a 
                                         % pattern like [_,_A,_,_A,_,_,_,_,_]
    Resp = case lists:foldl(
                     fun(X,A) when A == true andalso X >= $a andalso X =< $z -> A;
                        (_,_) -> false end, 
                        %% a function to test the set of character, you can call any function here
                     true,
                     C) of
               true -> ok;
               _ -> badNumCompte
            end,
    {Resp,C};
checkNumCompte(C) -> %% if the variable is not a list or has a bad length 
    {badNumCompte,C}.
于 2012-12-12T19:14:43.790 に答える