[erlang-questions] illegal guard expression with lists:nth

Pierpaolo Bernardi olopierpa@REDACTED
Thu Jun 11 11:50:58 CEST 2009


On Thu, Jun 11, 2009 at 11:37 AM, Roberto Ostinelli<roberto@REDACTED> wrote:
> dear all,
>
> why does the following expression give me an 'illegal guard expression' at
> compile?
>
> if
>        lists:nth(2, List) =:= "test" ->
> [...]
>
> lists:nth returns a term, thus i should be enabled to use it. on the
> contrary, the following expression is accepted:
>
> if
>        length(List) =:= 3 ->
> [...]
>
> am i missing a point?

You cannot use any function you want in a guard, but only a few BIFs.
length/1 is one of them, lists:nth/2 is not.

Check the manual for "guard sequences".

Ciao
P.


More information about the erlang-questions mailing list