[m-dev.] Adding default clauses to the language.
Ralph Becket
rbeck at microsoft.com
Wed Aug 8 20:48:53 AEST 2001
The following is a fairly common idiom in Mercury:
:- func f(string) = int.
f(S) = ( if f_2(S) = X then X else throw(...) ).
:- func f_2(string) = int is semidet.
f_2("foo") = 1.
f_2("bar") = 2.
f_3("baz") = 3.
Would it be a terrible thing to add the option of having
default clauses to the language, so one might write
:- func f(string) = int.
f("foo") = 1.
f("bar") = 2.
f("baz") = 3.
otherwise f(_) = throw(...).
Similarly, one might shorten det_map2 to
det_map2(_, [], [] ) = [].
det_map2(F, [X | Xs], [Y | Ys]) = [F(X, Y) | det_map2(F, Xs, Ys)].
otherwise det_map2(_, _, _) = throw("inputs of different lengths").
rather than having to duplicate the throw for the (F, [], [_|_])
and (F, [_|_], []) cases. It's not such a big deal for det_map2,
but having to catch each possible error case for det_map3 would
be real hassle; the programmer instead backs off to an if-then-else
which is less clear IMHO:
det_map3(F, Xs0, Ys0, Zs0) =
( if Xs0 = [], Ys0 = [], Zs0 = []
then []
else if Xs0 = [X | Xs], Ys0 = [Y | Ys], Zs0 = [Z | Zs]
then [F(X, Y, Z) | det_map3(F, Xs, Ys, Zs)]
else throw(...)
).
vs
det_map3(_, [], [], [] ) = [].
det_map3(F, [X | Xs], [Y | Ys], [Z | Zs]) = [F(X, Y, Z) | det_map3(Xs,
Ys, Zs)].
otherwise det_map3(_, _, _, _) = throw(...).
Thoughts?
- Ralph
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list