[erlang-questions] Bug ?!
Richard A. O'Keefe
ok@REDACTED
Wed Oct 4 02:38:07 CEST 2006
Richard Carlsson <richardc@REDACTED> reminds me of something:
Compare:
X * 4711
length(Stuff)
R#r.a
and
[X|Y]
{foo, X}
"abc" ++ Xs
Ever since I met them, I have *loved* n+k patterns in Haskell.
For example, if I want to define factorial, I can do
factorial 0 = 1
factorial n@(m+1) = n * factorial m
The semantics I want for this is
<pattern> + <constant> matches <value>
if (1) <value> >= <constant>
and (2) <pattern> matches <value> - <constant>
(Compile-time check: <constant> is a strictly positive integer)
I would really like to be able to write
factorial(0) -> 1;
factorial(N = (M+1)) -> N * factorial(M).
in Erlang. Note that the semantics I have defined for this makes
this SAFER than
factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
But also, when working with binary, ternary, and quaternary trees,
I have wanted to do this:
f(1) -> ...;
f(2*K+0) -> ...;
f(2*K+1) -> ....
where the semantics is
<multiplier> * <pattern> + <constant> matches <value>
if (1) <value> >= <constant>
and (2) (<value> - <constant>) mod <multiplier> = 0
and (3) <pattern> matches (<value> - <constant>) div <multiplier>
(Compile-time check: <constant> >= 0 and <multiplier> > 1, both integers.)
The special case of 2*_+_ can of course be done with bit tests and shifts.
This would certainly conflict with the use of function calls in patterns.
More information about the erlang-questions
mailing list