small erlang question.

R. meetegriot@REDACTED
Thu Jan 15 22:08:48 CET 2004


Hello, I just have an "erlang debutant" question. 

My question could be stated: is there a way to do a multiple condition
test with functions in conditions? (I am sure this is, but the
definition of 'if' and 'case' allows only Patterns (no function) and
guards (limited set of functions)!). I could use a list of conditions
and associated Funs, but I would like a more straightforward and
readable solution (if exists).

Note: the thing that brings me this question is:
I want to test if a string contains another from a static set of string.
My first (and incorrect) code would be:
if (string:str(Text,String)  /= 0) -> ...;
   (string:str(Text,String2) /= 0) ->...
end

but string:str is not accepted in a condition. 

I wrote a small function witch takes Text and a list of {Return_value,
String} which tries each string and return Return_value if it matches: 

find_strings(_, []) -> false;

find_strings(Texte,[{Retour,Chaine}|Liste])  ->
        T1 = string:str(Texte, Chaine),
        case T1 of
                0       -> find_strings(Texte,[Liste]);
                _Else   -> Retour
        end
        .

but I wanted to know if there was not an other elegant solution that
does not require a function.

Regards.

Raphaël.




More information about the erlang-questions mailing list