Re: [q-lang-users] A proposal to adopt Python tuple syntax
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2005-06-21 09:59:16
|
John.Cowan wrote: > Representing tuples as parenthesized expressions separated by commas is > a Good Thing, but 1-tuples (in languages where they are not the same as > non-tuples) are always a syntactic wart. Hi, John, I knew that someone was going to be bothered by this, sooner or later. ;-) Yes I know this wart oh so well, and spent countless hours on it. :( Identifying 1-tuples with their single members would certainly be the most elegant solution, but unfortunately you can't do that in a language where tuples are defined in a recursive fashion, so that you can conveniently write generic functions operating on tuples of arbitrary sizes. > The current Q solution to this wart is to interpret a parenthesized > expression as a 1-tuple only in certain circumstances: as noted in the > manual, "(99)" is a tuple but "(-99)" is not. Yes, it's certainly a pitfall for Q beginners (I teach Q in university courses so I'm very aware of this ;-). But, as a matter of fact, the rules are fairly clear: you get a 1-tuple by parenthesizing a primary expression, i.e., either an atomic expression or something which is already parenthesized, which is fairly well in line with common mathematical usage. Why would you want to write something like ((A-B)) to mean (A-B) anyway? The (-99) is _not_ a 1-tuple simply because -X is not an atomic expression. Now you could argue that ((-99)) is even uglier than (-99,), but I'm not with you there ... > I propose that the Python syntax be adopted instead. A Python tuple > is expressed in the same way as a Q one, except that there may be a > terminating comma after the last expression. In particular, "(expr,)" > is unambiguously a 1-tuple, allowing "(expr)" and "((expr))" to be merely > parenthesized expressions. The Python printer prints a terminating > comma only for 1-tuples. Been there, done that. ;-) IIRC, I had something like this in a very early version of the interpreter (pre-2.0), well before I was aware that Python does it the same way. But I can't help it, I think that this syntax is just plain ugly. Don't get me wrong: I'm all for good standard practices, so that ppl don't have to waste time to unneccesarily relearn something. But in this case I'd say that the (X,) notation is a wart and _not_ good standard practice. (Most, if not all, other FP languages avoid 1-tuples anyway, so there's no real "standard" here.) Actually, I found that the most common problem related to Q's 1-tuple syntax is that seasoned C or Pascal programmers write something like foo(99) instead of foo 99, because it's what they are used to. However, it's possible to relearn this, and if you want to learn a modern-style FP language, you have to learn the curried function application style anyway, so I don't see a real problem with this. Other opinions? I've thought about this problem for quite some time, but maybe there's another, prettier solution I missed? Cheers, Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikwissenschaft.uni-mainz.de/~ag |