For starters:
1. Quoting seems incosistent. Why is 'and' quoted but andalso is not?
2. Why is float division (/) not accepted in guards?
3. In the case of infix operators, does {'op','$1','$2'} always mean
"$1 op $2" (vs. "$2 op $1")?
4. The ERTS User's Guide lists get_tcw as an allowed function in ets
matches. Is that correct?
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions
On Nov 10, 2011 7:34 PM, "wrp" <i3t...@gmail.com> wrote:
>
> Match specifications don't appear to be complicated, but the
> documentation on them is sparse. I found the description in Cesarini &
> Thompson to be much clearer than in the ERTS User's Guide, but I still
> have some points of confusion.
>
> For starters:
>
> 1. Quoting seems incosistent. Why is 'and' quoted but andalso is not?
'and' and and are the same.
'andalso' and andalso are the same.
Just atoms is all it is.
>
> 2. Why is float division (/) not accepted in guards?
No clue, I did not know it was, I am curious too.
>
> 3. In the case of infix operators, does {'op','$1','$2'} always mean
> "$1 op $2" (vs. "$2 op $1")?
I think so, but do not quote me, wait for another to answer, I do not have the source right now.
>
> 4. The ERTS User's Guide lists get_tcw as an allowed function in ets
> matches. Is that correct?
>
No clue.
Probably a typo. Both need to be quoted, otherwise you get a syntax
error.
> 2. Why is float division (/) not accepted in guards?
Seems like an omission. It works for me:
1> ets:fun2ms(fun({A,B}) when A / B > 1 -> {A,B} end).
[{{'$1','$2'},[{'>',{'/','$1','$2'},1}],[{{'$1','$2'}}]}]
2> ets:new(foobar, [named_table]).
foobar
3> ets:insert(foobar, {1, 4}).
true
4> ets:insert(foobar, {10, 3}).
true
5> ets:select(foobar, v(1)).
[{10,3}]
> 3. In the case of infix operators, does {'op','$1','$2'} always mean
> "$1 op $2" (vs. "$2 op $1")?
Yes.
> 4. The ERTS User's Guide lists get_tcw as an allowed function in ets
> matches. Is that correct?
It doesn't seem so. ets:fun2ms accepts it, but using it in a select gives
a badarg:
8> ets:fun2ms(fun({_,_}) -> get_tcw() end).
[{{'_','_'},[],[{get_tcw}]}]
9> ets:select(foobar, v(8)).
** exception error: bad argument
in function ets:select/2
called as ets:select(foobar,[{{'_','_'},[],[{get_tcw}]}])
--
Magnus Henoch
Erlang Solutions Ltd
http://www.erlang-solutions.com/
http://www.erlang.org/doc/apps/erts/match_spec.html#id273975
dan
On Nov 11, 4:48 pm, Daniel Dormont <d...@greywallsoftware.com> wrote:
> Regarding 4). This is not abundantly clear on first read in the HTML
> version because the header doesn't stand out, but get_tcw is listed
> under "Functions allowed only for tracing", not "Functions allowed in
> all types of match specifications".
>
> http://www.erlang.org/doc/apps/erts/match_spec.html#id273975
>
> dan
>
On Nov 11, 6:44 am, Magnus Henoch <magnus.hen...@erlang-solutions.com>
wrote:
> > 1. Quoting seems incosistent. Why is 'and' quoted but andalso is not?
>
> Probably a typo. Both need to be quoted, otherwise you get a syntax
> error.
In the MatchConditions and MatchBody, aren't all the terms passed as
atoms?
Here are all the functions listed as allowed for ETS matches:
abs is_atom is_record '=:=' 'and'
element is_binary is_reference '=/=' 'or'
hd is_constant is_seq_trace '==' 'not'
length is_float is_tuple '/=' 'xor'
node is_function '+' '<' 'band'
round is_integer '-' '=<' 'bor'
size is_list '*' '>' 'bnot'
tl is_number 'div' '>=' 'bxor'
trunc is_pid 'rem' andalso 'bsl'
self is_port orelse 'bsr'
According to the Erlang rules for atoms, "An atom should be enclosed
in single quotes (') if it does not begin with a lower-case letter or
if it contains other characters than alphanumeric characters,
underscore (_), or @", so only the non-alphanumeric operators should
need quoting. Why do the arithmetic and logical operators need to be
quoted?
This has nothing to do with match specs specifically; as already
mentioned, there's a typo in that part of the manual. But in fact, you
cannot use 'and' or 'andalso' unquoted *anywhere* in Erlang if you
want them to be literal atoms rather than operators.
dan
On Nov 11, 8:29 pm, Daniel Dormont <d...@greywallsoftware.com> wrote:
> Erlang also has reserved words. If I were revising the manual, I would
> modify section 2.3 to explicitly say "or if it is a reserved word"
> with a link to the list of reserved words, which includes both 'and'
> and 'andalso'.
AH, HA! That's it, and the list of reserved words is:
after begin bsr cond if or rem
and bnot bxor div let orelse try
andalso bor case end not query when
band bsl catch fun of receive xor