[erlang-questions] Questions on match specifications

Magnus Henoch magnus.henoch@REDACTED
Fri Nov 11 15:44:43 CET 2011


> 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?

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/




More information about the erlang-questions mailing list