1

我从二郎开始

目前我有一个函数,它以帐号为参数

在这个函数中我做了测试:

测试这个数字是否为空 测试字符数是否等于 9 字符

测试这些字符是数字还是字母

这个函数的结构是:

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

我认为我们应该开发一个子功能第一个是验证字符的数字第二个是验证字符的格式

最好的问候

艾伦

4

3 回答 3

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 回答
0

谢谢你的回答,我试试这个代码:

-export([check_num_compute/1]).
check_num_compute(NumeroCompte) when length(NumeroCompte) == 9->
    case io_lib:printable_unicode_list(NumeroCompte) of
        true -> validate_contents(NumeroCompte);
        false -> {error, bad_arg}

    end;
check_num_compute(_) ->
io:format("count number incorrect\n").
validate_contents([]) -> io:format("count number coorect\n"); 
validate_contents([C|Cl]) when C >= $0 ,C  =< $9 ->
validate_contents(Cl);
validate_contents(_) ->io:format("count number correct but it s not only number\n"). % if any character isn't a number than bad_arg,

但是此代码不会测试 NumeroCompte 是否为空

当它为空时,我想显示此消息:

帐号为空

而且我想知道如何做同样的事情(以显示我的消息(正确,空,不正确..)与您的响应的第二个代码:

check_num_compute_version2(NumeroCompte2) when length(NumeroCompte2) == 9->
    case io_lib:printable_unicode_list(NumeroCompte2) of
        true -> validate_contents2(NumeroCompte2);
        false -> {error, bad_arg}
    end;
check_num_compute_version2(_) ->
    {error, bad_arg}.

validate_contents2(NumeroCompte2)->
    [] == lists:dropwhile(fun(C)->C >= $0 andalso C =< $9 end,
                          NumeroCompte2).
于 2012-12-13T14:29:35.613 回答
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 进行简单更改就足够了。

此外,与 list: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 回答