[Haskell-cafe] Re: monad subexpressions

ok ok at cs.otago.ac.nz
Sun Aug 5 22:02:17 EDT 2007

On 4 Aug 2007, at 12:41 am, Mirko Rahn wrote:

>>>> rewrite *p++=*q++ in haskell?
>> it's one of C idioms. probably, you don't have enough C experience to
>> understand it :)
> Maybe, but how can *you* understand it, when the standard is vague  
> about it?
> It could be
> A: *p=*q; p+=1; q+=1;
> B: *p=*q; q+=1; p+=1;
> C: tp=p; tq=q; p+=1; q+=1; *tp=*tq;
> ...and so on. Which is the "right" version?

The standard makes it perfectly clear that they ALL are,
as they all produce exactly the same effect.  The only case
where the exact translation could matter is when the two
variables are the same, which the standard forbids (and yes,
there are static checkers for that, and smart C programmers
use them).

Some message I didn't see suggested that good C programmers
use memcpy() rather than *p++ = *q++.  I would point out that
the assignment form is type checked and memcpy() is not, so
memcpy() is not always to be preferred.

> readability depends on the reader as your C example shows. Some  
> Haskellers would very quickly recognize some common idioms, where  
> others need some help...

*p++ = *q++ is indeed a C idiom and it is presented and described in the
classic C book by Kernighan & Ritchie, which one expects competent C
programmers to have read.

C does indeed have grave weaknesses, but there are better targets
for derision than this.

More information about the Haskell-Cafe mailing list