我在网上看到了这个代码:
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.
Run Code Online (Sandbox Code Playgroud)
它是我alwais梦寐以求的守卫,因为它检查输入是否是一个字符串 - 但是,我不允许在erlang中使用它,为什么会这样?是否有解决方法?
我想能够写下这样的东西:
Fun(Str) when is_string(Str) -> Str;
Fun(Int) when is_integer(Int) -> io:format("~w", [Int]).
Run Code Online (Sandbox Code Playgroud)
甚至更好地在消息上使用它.
jur*_*uro 10
您不能在警卫中使用用户定义的功能.这是因为警卫中的功能必须没有副作用(例如io:format在你的功能中使用).在警卫中,您仅限于以下内容:
is_atom,is_constant,is_float,is_integer,is_list,is_number,is_pid,is_port,is_reference,is_tuple,is_binary,),is_functionis_recordnot,and,or,andalso,orelse,,,;),>,>=,<,=<,=:=,==,=/=,/=),+,-,*,div,rem),band,bor,bxor,bnot,bsl,bsr),abs/1,element/2,hd/1,length/1,node/1,2,round/1,size/1,tl/1,trunc/1,self/0)| 归档时间: |
|
| 查看次数: |
1060 次 |
| 最近记录: |