【erlang】数组内是否存在某元素、不用api实现

    技术2022-07-21  68

    数组内是否存在某元素

    api方法:lists:member

    L = [1,2,3,4,5]. %% lists:member 返回 true 或者 false lists:member(2,L). %%结果是true

     

    列表推导

    [H || H <- L, H == 2 ].

     

    其他方法

    方法一 case of

    exist([H|T],Ch)-> case H == Ch of true -> true; false -> exist(T,Ch) end; exist([],_)-> false.

    方法二 if

    aexist([H|T],Ch)-> if H == Ch -> true; H /= Ch -> aexist(T,Ch) end; aexist([],_) -> false.

     

    过滤字符串

    %过滤字符串 --方法 afilter([],_) -> []; afilter(H,Ch) -> [A || A <- H , A /= Ch ].

    Processed: 0.008, SQL: 9