Wither Self
Raimo Niskanen
raimo@REDACTED
Tue Sep 9 09:23:22 CEST 2003
If the compiler would check for unbound variables in the right side of
match expression *after* the pattern match, how would the following be
treated?
{Even, Odd, Strange} =
{fun(0) -> true; (N) -> Odd( N-1) end,
fun(0) -> false; (N) -> Even(N-1) end,
Strange},
when Strange is bound vs unbound, or
{Even, Odd, Strange} =
{fun(0) -> true; (N) -> Odd( N-1) end,
fun(0) -> false; (N) -> Even(N-1) end,
{very,Strange}},
or even
{Even, Odd, Strange} =
{fun(0) -> true; (N) -> Odd( N-1) end,
fun(0) -> false; (N) -> Even(N-1) end,
case Strange() of
true -> fun()->Odd(0)end;
false -> fun()->Even(0)end
end},
or do you mean that it is just fun()s unbound variables that should be
checked after the pattern match binding?
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
Richard A. O'Keefe wrote:
> Joe Armstrong <joe@REDACTED> wrote:
> Shouldn't there be a Self in funs?
>
> Fac = fun(0) -> 1;
> (N) -> N*Self(N-1) end
>
> Well, if the compiler were just a touch smarter, you wouldn't need it.
>
> If you have
> <pattern> = ... <fun> ...
> then the check for unbound variables in <fun> should ask whether
> any variable will still be unbound *after* the pattern is matched
> instead of whether any variable is unbound *before* the pattern match.
>
> Then you could just write
>
> Fac = fun(0) -> 1; N -> N * Fac(N-1) end,
>
> without needing Self. This would also handle mutual recursion:
>
> {Even, Odd} =
> {fun(0) -> true; (N) -> Odd( N-1) end,
> fun(0) -> false; (N) -> Even(N-1) end},
>
> which the Self proposal would not deal with at all.
>
More information about the erlang-questions
mailing list