我來到防空火炮在網絡上此代碼:我可以在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.
它是保護我alwais夢見掉,因爲它會檢查是否輸入是一個字符串 - 怎麼過的,我不能用它在erlang中,這是爲什麼?有沒有解決辦法?
我希望能夠寫這樣的東西:
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:// stackoverflow.com/questions/6505213/is-there-a-way-to-use-local-function-in-guard),[this](http://stackoverflow.com/questions/6927632/checking-for-membership -in-erlang-guard),[this](http:// stackoverflow。/功能/不能使用功能調用保護)和[this](http://stackoverflow.com/questions/7474894/use-of-function-in-guard-not -allowed-suggestions-for-alternate-implementation-w)問題。 – legoscia