Re: [q-lang-users] Complex numbers
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2006-06-13 09:54:12
|
John Cowan wrote: > Albert Graef scripsit: >>unparse (comp_rect X Y) = '(X+Y*i); > > Sounds excellent to me. Of course, there would be the risk that > whatever the pretty-printer produced would not in fact parse > and evaluate to the same thing, but that's the price you pay > for sharp tools. I've implemented this (in cvs now) and it works very nicely indeed. Here's a little example (Rob, you should find that interesting, too): type Rat = private const rat P Q; public (over) P Q @ (/); P:Int over Q:Int = rat (P div D) (Q div D) where D = gcd P Q; unparse (rat P Q) = '(P over Q); ==> 2 over 6 // the result is actually rat 1 3, which is unparsed as: 1 over 3 Note how the `over' operator behaves like a constructor (with equations) for Rat objects. The real constructor `rat' is never seen outside the module where the type is defined and can thus be hidden by making it private. Neat, isn't it? :) The application to complex.q and rational.q is obvious. But I'm also considering equipping the standard container types with unparsing rules, e.g., the result of dict [(1,2),(3,4)] would then actually be printed as dict [(1,2),(3,4)]. Any objections? Also, I'm about to turn complex numbers into an algebraic type now. If anyone wants to complain, now is your last opportunity. ;-) 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 |