[q-lang-users] New stuff in cvs: multichar ops, views
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2007-05-30 11:59:30
|
Hi all, I had some time to work on the new Q release over the Whitsun holidays. As a result, you can find some interesting new stuff in cvs today: Multichar operator symbols: These have been asked for on the mailing list a while ago. I've given in to the popular demand now and implemented them. So you can write, e.g.: public (--) Xs Ys @(-); Xs:List--Ys:List = foldl (flip (filter . neq)) Xs Ys; (Q's lexical syntax had to be revised to support this, but most old scripts should be unaffected.) Wadler/Okasaki-style views (good stuff!): These are now implemented, too. Virtual constructors can be declared with the new 'virtual' keyword and can then be used in pattern-matching definitions like real constructors (if the appropriate views a.k.a. unparsings are defined). Note that in order to go with the customary terminology, I renamed the builtin 'unparse' routine to 'view' so you will have to change your scripts accordingly. For example, here is how the 'Rational' type and its view are now defined in the standard library: // from rational.q public type Rational : Real = virtual (%) P Q @ (/) | private const rat N D; // from prelude.q view Q:Rational = '(N % D) where (N:Int,D:Int) = num_den Q; Having (%) as a virtual constructor of 'Rational' lets you use the operator in your definitions just like a real constructor, e.g.: def X%Y = 3%14 + 7%6; foo (X%Y) = (X,Y); This yields: ==> (X,Y); foo (4%6) (29,21) (2,3) The same applies to the container ADTs in the standard library. E.g.: def set Xs = set [1..10]+set [5..12]; mymembers (set Xs) = Xs; Which yields: ==> Xs; mymembers (set ["a".."c"]+set ["A".."C"]) [1,2,3,4,5,6,7,8,9,10,11,12] ["A","B","C","a","b","c"] It goes without saying that this makes the handling of abstract data types *much* more convenient and elegant. (Note that 'def's in the interactive command loop of the interpreter don't quite work like that yet, as they don't handle virtual constructors right now; but I'm working on that.) Enjoy! :) 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 |