Newsgroups: alt.lang.design,comp.lang.c++,comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!bloom-beacon.mit.edu!uhog.mit.edu!news.mathworks.com!hookup!olivea!uunet!allegra!alice!ark
From: ark@research.att.com (Andrew Koenig)
Subject: Re: Comparing productivity: LisP against C++ (was Re: Reference Counting)
Message-ID: <D17u68.Jpz@research.att.com>
Organization: Software Engineering Research Department
References: <D0xAIp.3Dn@rheged.dircon.co.uk> <vrotneyD11MDv.Ks7@ <19941221.141340.599592.NETNEWS@UICVM.UIC.EDU>
Date: Thu, 22 Dec 1994 14:13:20 GMT
Lines: 64
Xref: glinda.oz.cs.cmu.edu comp.lang.c++:104562 comp.lang.lisp:16158

In article <19941221.141340.599592.NETNEWS@UICVM.UIC.EDU> dhanley@matisse.eecs.uic.edu (David Hanley) writes:
> David Hanley (dhanley@picasso)

>         In fact, even better:

> void Reverse( char *f1 , char *f2 )
> {
>     FILE *in = fopen( f1 , "r" );
>     FILE *out = fopen( f2 , "w" );
> 
>     int i;
>     while( fread( &i , 4 , 1 , in ) == 1 )
>         {
>              i ^= 0xffffffff;
>              fwrite( &i , 4 , 1 , out );
>         }
>     fclose( in );
>     fclose( out );
> }

Not better, actually, because it relies on an int being 4 bytes
of 8 bits each.  If you're going to use this strategy,
the most portable is probably something like

	int c;

	while ((c = getchar()) != EOF)
		putc(~c);

If I were using a C++ system that supported STL (the new template
library that has been accepted as part of the C++ standard)
I might be tempted to write something like this:

	input_iterator<char> in(cin), eof;
	output_iterator<char> out;

	transform(in, eof, out, flip());

where flip is defined as follows:

	class flip: public unary_function<char, char> {
	public:
		char operator() (char) {
			return ~x;	// or whatever you like
		}
	};

The class flip is needed only because the library doesn't include a
similar class to do unary ~ directly.  If you're willing to use unsigned
chars and settle for the transformation that takes x into
(~(unsigned char)0)-x, there is a cleverer way to do it:

	input_iterator<unsigned char> in(cin), eof;
	output_iterator<unsigned char> out;

	transform(in, eof, out,
		bind1st(minus<unsigned char>(), (~(unsigned char)0)));

I think this compares favorably in complexity with the Lisp versions.
I also think it proves nothing -- the problem is too simple and doesn't
seem to hit any major issues.
-- 
				--Andrew Koenig
				  ark@research.att.com
