Is there way how to define combined patterns like range and odd without define extra new patter for it.<br><br><div class="gmail_quote">On Mon, Feb 23, 2009 at 1:49 AM, Richard O'Keefe <span dir="ltr"><<a href="mailto:ok@cs.otago.ac.nz">ok@cs.otago.ac.nz</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;"><div class="Ih2E3d"><br>
On 21 Feb 2009, at 12:01 am, Zvi wrote:<br>
<br>
><br>
> I like Abstract Patterns, I think they make patterns more like first-<br>
> class<br>
> citizens and make ADTs implementation easier. There several<br>
> comments, which<br>
> I'll write later. For now, I just trying to write simple abstract<br>
> pattern<br>
> for inclusive range of integers:<br>
><br>
> #range(N,M) when is_integer(X) -> N=<X, X<=M.<br>
><br>
> I just introduced X. Is this right way?<br>
<br>
<br>
</div>An abstract pattern lets you name and reuse a combination of<br>
pattern match and type test that you could have written without it.<br>
<br>
The form of a pattern definition is<br>
        #<head> when <guard> -> <pattern>.<br>
<br>
In this case, N =< X, X =< M is a guard, not a pattern.<br>
I think what you want is<br>
<br>
        #range(L, U, X)<br>
        when is_integer(X), L =< X, X =< U<br>
        -> X.<br>
<br>
Without abstract patterns, you would write<br>
        f(..., X, ...) when is_integer(X), L =< X, X =< U ... -><br>
               ^rhs         ^guard......................^<br>
The bit you would have written in the head of a function clause<br>
goes on the right of the arrow, and the bit that would have<br>
been in the guard of a function clause goes in the guard of the<br>
pattern.<br>
<div class="Ih2E3d"><br>
> Do I need to specify is_integer(N), is_integer(M) for abstract pattern<br>
> arguments too ?<br>
<br>
</div>Only if you want to.  The one thing you cannot do is solve for<br>
the bounds.  Any variable in the head of a pattern that does not<br>
occur in the right hand side must be bound some other way.<br>
So you can do<br>
<br>
        f(N, #range(1,N, X)) -> ...<br>
or      f(N, #range(1,N, 42)) -> ...<br>
but not<br>
        g(#range(1,N, 42)) -> ... N ...<br>
<div><div></div><div class="Wj3C7c"><br>
<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>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br><br>Analyze your data in minutes. Share your insights instantly. Thrill your boss.  Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com">www.gooddata.com</a><br>