[erlang-questions] Guards?

Kostis Sagonas kostis@REDACTED
Tue Apr 5 14:38:55 CEST 2016


On 04/05/2016 02:27 PM, Roberto Ostinelli wrote:
> I'm a little stumped and I feel I'm missing something sooooooo beginner
> here.
>
> 1> F = fun(X) when (is_integer(X) or is_float(X)) and X < 5 -> ok end.
> #Fun<erl_eval.6.50752066>
> 2> F(1).
> ** exception error: no function clause matching
> erl_eval:'-inside-an-interpreted-fun-'(1)
> 3> F2 = fun(X) when (is_integer(X) or is_float(X)) and (X < 5) -> ok end.
> #Fun<erl_eval.6.50752066>
> 4> F2(1).
> ok
>
> ...Can some kind soul clarify?

It's a long story.  Its short version is that you will have a much 
better state of mind if you forget the presence of 'or' and 'and' in 
guards and use ';' and ',' when you can, and 'orelse' and 'andalso' when 
you cannot (as in your F fun).

1> F = fun(X) when (is_integer(X) orelse is_float(X)) andalso X < 5 -> 
ok enD.
#Fun<erl_eval.6.50752066>
2> F(1).
ok


Kostis



More information about the erlang-questions mailing list