<br><br><div class="gmail_quote">On Thu, Oct 30, 2008 at 5:25 PM, Francois De Serres <span dir="ltr"><<a href="mailto:fdeserres@gmx.net">fdeserres@gmx.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'll investigate further both of these options:<br>
<br>
A) Match Specifications with ets:match_spec_run/2 (thanks to Michael Radford):<br>
slightly awkward to use (match spec syntax), but straightforward to implement.<br>
<<a href="http://www.erlang.org/doc/apps/erts/match_spec.html" target="_blank">http://www.erlang.org/doc/apps/erts/match_spec.html</a>><br>
<br>
B) ad-hoc matching functions generated with erl_eval (thanks to Richard Carlsson):<br>
metaprogramming magic, but the resulting API is lean.<br>
8<--------<br>
make_match_fun(Pstr) -> element(2, erl_eval:expr(hd(element(2,<br>
erl_parse:parse_exprs(element(2,<br>
erl_scan:string(lists:flatten(io_lib:format("fun (~s=__P) -><br>
{matched,__P}; (_) -> fail end.", [Pstr]))))))), erl_eval:new_bindings())).<br>
F = make_match_fun("{X,Y}")<br>
F({1,2}) ==> {matched, {1,2}}<br>
F({}) ==> fail<br>
8<--------<br>
<br>
Thanks to all for your precious help,<br>
--<br>
François<br>
<br>
</blockquote><div><br>One of most important thing in Erlang is reliability and readability which goes in Erlang together. Compare this code and yours:<br><br>make_match_fun(Pstr) -><br>    FStr = lists:flatten(io_lib:format(<br>
        "fun (~s=__P) -> {matched,__P}; (_) -> fail end.", [Pstr]<br>      )),<br>    {ok, Tokens, _Line} = erl_scan:string(FStr),<br>    {ok, [Expr]} = erl_parse:parse_exprs(Tokens),<br>    {value, Fun, _} = erl_eval:expr(Expr, erl_eval:new_bindings()),<br>
    Fun.<br><br>Difference is not only you can see what is going on, but if any fail, you get valuable error message. It fails fast and as result code is more reliable.<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
> Hi there.<br>
><br>
> I need a function that actually does pattern matching: pm(Tuple,<br>
> Pattern)->MatchedPattern|error<br>
> > pm({a,b,c}, {a,X,Y}).<br>
> {a,b,c}<br>
> >X.<br>
> b<br>
> > pm({a,b,c}, {b,X,Y}).<br>
> {error, badmatch}<br>
> > pm({a,b,c}, {a,X}).<br>
> {error, badmatch}<br>
> etc.<br>
><br>
> This trivial code works only with the empty tuple/pattern :<br>
> pm(T,P) -><br>
>     P = T, P.<br>
> because pattern variables (X, Y) are unbound at function call.<br>
><br>
> I tried to find a hint in the libs with no avail.<br>
> If anyone has a lead, can you please put me on track?<br>
><br>
> Thanks,<br>
> --<br>
> Francois<br>
><br>
> --<br>
> "Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...<br>
> Jetzt GMX TopMail testen: <a href="http://www.gmx.net/de/go/topmail" target="_blank">http://www.gmx.net/de/go/topmail</a><br>
> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
--<br>
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...<br>
Jetzt GMX TopMail testen: <a href="http://www.gmx.net/de/go/topmail" target="_blank">http://www.gmx.net/de/go/topmail</a><br>
<font color="#888888"><br>
--<br>
GMX Download-Spiele: Preizsturz! Alle Puzzle-Spiele Deluxe über 60% billiger.<br>
<a href="http://games.entertainment.gmx.net/de/entertainment/games/download/puzzle/index.html" target="_blank">http://games.entertainment.gmx.net/de/entertainment/games/download/puzzle/index.html</a><br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br>