2012-12-12 36 views
1

的我開始使用Erlang二郎功能:驗證參數

格式和目前我有一個函數,作爲參數的賬號

,並在這個函數中我做了測試:

測試,如果這是空號或不 測試的字符數是否等於9或不字符

測試,如果這些字符是數字或字母

這個函數的結構是:

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

我認爲,我們應該制定一個子功能,第一個是驗證號碼FO charactersthe第二個是驗證字符格式

最好reagrds

阿倫

+0

我編輯我的答案,以更好地匹配您所需的錯誤消息,見下面 – Jr0

回答

0

在二郎山的使用是用試圖匹配模式變量和使用警衛。例如,你給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}. 
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應該足夠了。

而且,你可能更喜歡下面的列表:dropwhile/2做法:

validate_contents([]) -> 
    true; 
validate_contents([C|Cs]) when C >= $0, C =< $9 -> 
    validate_contents(Cs); 
validate_contents(_) -> 
    {error, bad_arg}. 
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是否爲空

當它爲空時我想顯示此消息:

帳戶號碼爲空

而且我想知道如何dothe同樣的事情(與響應的第二代碼,以顯示我的消息(coorect,空的,不正確的..):

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). 
+0

代碼測試它是否是除了9以外的任何長度。我不知道爲什麼要打印出這些特定的錯誤消息,我只是打印出用法(規則對於任何{error,bad_arg}條件 - 爲什麼要讓用戶猜測正確的用法。另外,有人可能會認爲我的代碼是廢話,但希望只需很少的工作就可以理解我所做的事情,應該很容易根據需要對其進行修改。如果你發現我的答案很有希望,請接受它或者投票給它。 – Jr0

+0

你可以在開始時添加一個測試:check_num_compte([]) - > {error,empty_code);當is_list(L)和長度(L)=/= 0 - > {error,wrong_length)時,check_num_compte(L) ...然後是您的版本2.請注意,如果arg不是列表,則長度(Arg)== 9將失敗,因此您將按預期得到bad_arg。 – Pascal