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

Bob Ippolito <>
Wed Feb 24 04:58:51 CET 2010


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 <> 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, 钱晓明 <> 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.
>


More information about the erlang-questions mailing list