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

Michael Turner leap@REDACTED
Sun Feb 14 13:07:09 CET 2010


Could be worse, y'know.  At least Erlang "if" guards are checked in
the order written.  Dijkstra introduced the term "guard" using if ...
syntax as well, but if more than one guard was true, one of the guarded
statements was selected "non-deterministically" -- at least if you
believe Wikipedia.

   http://en.wikipedia.org/wiki/Guarded_Command_Language

On the whole, I'd prefer the guards at Guantanamo.

-michael turner


On 2/14/2010, "Jayson Vantuyl" <kagato@REDACTED> wrote:

>Oddly, in Erlang, "if" only accepts expressions that are valid in
>guards. This usually means special builtin functions that are normally
>used to make pattern matching more precise. Because pattern matching
>must be fast, most function calls are forbidden.
>
>For this reason, you'll usually see a case (or a try) statement where
>you'd expect an "if".
>
>To make thing even more confusing, "if" is most useful when you have a
>number of unrelated comparisons (limited to guards) that should be
>checked until one is true. Most other languages would use a chain of
>if-elsif-else statements or even a case statement.  E.g. "case
>filelib:is_dir(H) of true -> ...; false -> ... end"
>
>To top it off, you could save that expression in a variable and that
>could work, but would still be confusing. E.g. "F = filelib:is_dir(H),
>if F == true -> ...; true -> ... end"
>
>This is all a but backwards and probably more confusing than it has a
>right to be.  That said, you should probably read the section of the
>Erlang manual about guards. Once you grok them, they're powerfully
>useful. Until then, just remember that "if" is for guards only, and
>you probably want a case (or try) statement.
>
>Good luck!
>
>Sent from my iPhone
>
>On Feb 14, 2010, at 2:51 AM, Kay Kay <kaykay.unique@REDACTED> 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 ?
>>
>>
>>
>>
>>
>>
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>>
>
>________________________________________________________________
>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