は、私は、Web上でこのコードaccrossに来た:私は自分のガードをErlangで作ることができますか?
is_char(Ch) ->
if Ch < 0 -> false;
Ch > 255 -> false;
true -> true
end.
is_string(Str) ->
case is_list(Str) of
false -> false;
true -> lists:all(is_char, Str)
end.
ITSは、それが入力文字列をあるかどうかを確認することで、私は、オフ夢見alwaisガードです - どのように今まで、私はそれを使用することはできませんよエルランでは、なぜこれは?そして、回避策はありますか?
私のようなものを書くことができるようにしたいと思います:
Fun(Str) when is_string(Str) -> Str;
Fun(Int) when is_integer(Int) -> io:format("~w", [Int]).
またはより良いメッセージにそれを使用しています。
文字列をテストするより良い方法は、 'io_lib:printable_list/1'と' io_lib:printable_unicode_list/1'を組み合わせて使う方法です。 –
[this](http://stackoverflow.com/questions/10861347/why-comparing-function-results-is-an-illegal-guard-exception-in-erlang)[this](http:// [this](http://stackoverflow.com/questions/6927632/checking-for-membership)、[this](http://stackoverflow.com/questions/6927632/checking-for-membership) -in-an-erlang-guard)、[this](http:// stackoverflow。com/questions/2241340/function-call-in-function-guard)と[this](http://stackoverflow.com/questions/7474894/use-of-function-in-guard-not -allowed-suggestions-for-alternate-implementation-w)の質問です。 – legoscia