[q-lang-users] RFC: Conditional syntax
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2006-05-30 23:11:57
|
Hi all, I've completed: memoized special arguments; call-by-need pattern matching; and a built-in lambda implementation -- along with the usual kind of syntactic sugar, i.e., "\X.Y". As a wanted side-effect, lambdas also work much faster now, and correctly deal with the anonymous variable and non-linear patterns. This is all in CVS now. For the next RC I still have one item on my TODO list: conditionals. For this, I'm seeking your advice and comments. First, I'd like to add sugar for the ifelse function ("if X then Y else Z"). That should be rather straightforward, but I still have to figure out how to add that to the syntax without making the grammar ambiguous. But do we really need/want this? Or is everyone happy with "ifelse X Y Z" as it is? Second, I'd like to replace the multiway and matching conditionals in the standard library. These look awfully C-stylish and verbose right now (the following examples are somewhat silly since you'd probably do them with conditions and patterns on the equation level instead, but they are for illustrative puposes only): (Ex. 0) abs X = switch (case (X<0) (-X), default X); empty Xs = match Xs (case [] true, default false); So this what we have right now. I'd like to replace this with something more tidy and Lisp-like. Maybe this: (Ex. 1) abs X = cond ((X<0, -X), (true, X)); empty Xs = case Xs (([], true), (_, false)); Or maybe the sequence of clauses should actually be a stream instead of a tuple? (Ex. 2) abs X = cond {(X<0, -X), (true, X)}; empty Xs = case Xs {([], true), (_, false)}; This has the advantage that the sequence of clauses could be a non-special argument (since the expressions inside of it are already protected by the stream constructor), and that we could even have infinite lists of clauses. I'm also considering to add some unobtrusive syntactic sugar to allow "grouping" inside tuples, lists and streams. E.g., [X1,Y1;X2,Y2;...] would be the same as [(X1,Y1),(X2,Y2),...]; this would also be useful, e.g., for writing lists of key-value pairs. With this notation, the above could be rewritten as: (Ex. 3) abs X = cond (X<0, -X; true, X); empty Xs = case Xs ([], true; _, false); Which, IMHO, is quite tidy and also looks ok when formatted in two columns, e.g.: empty Xs = case Xs ( [] , true; _ , false; ); Incidentally this also closely resembles the corresponding constructs in ML and Haskell (minus some of the sugar, of course). So what do you prefer? Multiple choices possible. :) 0. Keep the old definitions (Ex. 0). 1. Clauses as a tuple of (guard/pattern, value) pairs (Ex. 1). 2. Clauses as a stream of (guard/pattern, value) pairs (Ex. 2). 3. 1 or 2 with "grouping" sugar (Ex. 3). 4. Don't care. 5. Any other ideas? I'm currently leaning towards 2+3. Comments appreciated... Cheers, Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |