[erlang-questions] illegal guard expression: filelib:is_dir(H)

Kostis Sagonas <>
Sun Feb 14 12:01:22 CET 2010


Kay Kay wrote:
> I have a function along the following lines
> 
> do_process_elements([],  _) ->   [];
> do_process_elements([H | _], Fn) ->
>   if
>     filelib:is_dir(H) ->
>       process_directory(H, Fn);
>     true ->
>       process_file(H, Fn)
>   end.
> 
> 
> When I compile - I get the following error,  where the line number 
> refers to    - filelib:is_dir(H) function.
> 
> ./test.erl:23: illegal guard expression
> 
> Given that filelib:is_dir(H) returns true / false  - I thought this 
> would fit in here as a predicate. What could be going wrong ?

The if construct in Erlang is very restrictive w.r.t. what it can be 
used with.  Do not use it.  Use case instead:

  do_process_elements([],  _) ->   [];
  do_process_elements([H | _], Fn) ->
    case filelib:is_dir(H) of
      true ->
        process_directory(H, Fn);
      false ->
        process_file(H, Fn)
    end.

Kostis


More information about the erlang-questions mailing list