[erlang-questions] guards

Michael Truog mjtruog@REDACTED
Sat Jul 25 05:28:21 CEST 2009

- Can't use parentheses in guards
- All the guards are in the erlang module and are explicitly documented
as allowed in guard expressions
  (usually is_something/1, erlang:length/1 works but supposedly is O(n),
not O(1) like the others)
- use "," for and, otherwise ";" for or... they evaluate in a sequential way
- Guards help to catch errors early and give hints for optimizations, if
they can occur
- Specifying the type in the function argument is always faster than
adding a guard function for the type
   (as seen in the "The is_record() guard" thread)

Dave Pawson wrote:
> filterHelper(Lst,Int,Result) when ((length(Lst) == 1) and
> (lists:nth(1,Lst) =< Int)) ->
>     [lists:nth(1,Lst) | Result];
> and I'm told
> ./math4.erl:132: illegal guard expression
> I believe I can't use 'user defined' references in a guard statement,
> but I'm unsure if  lists: module is such a statement?
> Suggestions please

More information about the erlang-questions mailing list