[erlang-questions] Why it is a illegal guard expression?

Michael Truog mjtruog@REDACTED
Wed Feb 24 06:12:26 CET 2010


Guards are the only statements in Erlang that do not have side-effects
and they should be faster when evaluated in guard expressions.  So there
are reasons to use if statements for guard expressions.  However, most
of the time you must use case statements, since very few functions can
be used within guard expressions.  Using an if statement can keep the
code a little bit more pure, by avoiding residual statefulness.

Bob Ippolito wrote:
> Additionally, there are very few reasons to use "if". In general you
> should be using "case", which does not have this problem.
>
> process_filelist([File|FileList], Files)->
>     case filelib:is_dir(File) of
>         true ->
>             {ok, NewFileList} = file:list_dir(File),
>             process_filelist(NewFileList, Files);
>         false ->
>             process_filelist(FileList, [File|Files])
>     end;
> process_filelist([], Files) ->
>     Files.
>
> On Tue, Feb 23, 2010 at 7:48 PM, Michael Richter <ttmrichter@REDACTED> wrote:
>   
>> There is a very limited list of which functions can be used in guard
>> expressions.  From
>> http://www.erlang.org/doc/reference_manual/expressions.html#id2273702 we
>> have:
>>
>> The set of valid guard expressions (sometimes called guard tests) is a
>>     
>>> subset of the set of valid Erlang expressions. The reason for restricting
>>> the set of valid expressions is that evaluation of a guard expression must
>>> be guaranteed to be free of side effects.
>>>       
>> We also find a list of functions which can be used in guard expressions
>> there.
>>
>> On 24 February 2010 11:42, 钱晓明 <kyleqian@REDACTED> wrote:
>>
>>     
>>> Hi, I am a newer of erlang. I wrote a module to find all files(no
>>> directory)
>>> under a directory, including its subdirectory:
>>> -module(lib_test).
>>> -export([list_files/1]).
>>>
>>> list_files(Path)->
>>>    {ok, FileList} = file:list_dir(Path),
>>>    process_filelist(FileList, []).
>>>
>>> process_filelist([File|FileList], Files)->
>>>    if
>>>       filelib:is_dir(File) ->
>>>        {ok, NewFileList} = file:list_dir(File),
>>>        process_filelist(NewFileList, Files);
>>>       true ->
>>>        process_filelist(FileList, [File|Files])
>>>    end;
>>> process_filelist([], Files) ->
>>>    Files.
>>>
>>> But I fail to compile this file, because " filelib:is_dir(File) ->" is
>>> illegal guard expression. So Why it is illegal? What should I write?
>>> Thanks for your help!
>>>
>>>       
>>
>> --
>> "Perhaps people don't believe this, but throughout all of the discussions of
>> entering China our focus has really been what's best for the Chinese people.
>> It's not been about our revenue or profit or whatnot."
>> --Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.
>>
>>     
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>
>   



More information about the erlang-questions mailing list