Is there an elegant guard test for strings?

Vance Shipley vances@REDACTED
Tue Aug 16 23:35:29 CEST 2005


Unfortunately you can't create your own guards.
But you can create your own is_string/1 and use
it like:

    foo(X, is_string(X)).


foo(X, true) ->
    blah()...
foo(X, false) ->
    lists:map(fun(Z)->foo(Z) end,X).

is_string([]) ->
    true;
is_string([H|T]) when H >= $ , H =< $~ ->
    is_string(T);
is_string(_) ->
    false.


On Wed, Aug 17, 2005 at 07:09:34AM +1000, Tony Hobbins wrote:
}  
}  foo(X) when is_string(X)->
}  	blah();
}  
}  foo(X) when is_list(X)->
}  	lists:map(fun(Z)->foo(Z) end,X).



More information about the erlang-questions mailing list