divRem by `-' performance

Serge D. Mechveliani mechvel at botik.ru
Wed Oct 17 17:37:38 CEST 2012


On Wed, Oct 17, 2012 at 06:17:28PM +0300, Roman Cheplyaka wrote:
> * Serge D. Mechveliani <mechvel at botik.ru> [2012-10-17 19:02:37+0400]
> > People,
> > consider the following contrived program for division with remainder:
> > 
> > ----------------------------------------------------------------
> > qRem :: Int -> Int -> (Int, Int)
> > qRem m n = if m < 0 || n <= 0 then  error "\nwrong arguments in qRem.\n"
> >            else                     qRem' 0 m
> >            where
> >            qRem' q r = if r < n then (q, r)  else qRem' (succ q) (r - n)
> 
> You need to force evaluation of 'q' here, otherwise it becomes a growing
> chain of 'succ' applications.
> 
> E.g.
> 
>     qRem' q r = if r < n then (q, r)  else (qRem' $! succ q) (r - n)


This looks as a natural explanation.
But it is generally difficult for me to admit that sometimes it is 
desirable to use strinctess annotation.
I programmed DoCon for 6 years, and it does not have any bit of 
strictness annotation, and I always had an impression that it is all
right with performance.
And now it occurs that setting  $!  in some places may make many parts
of it somewhat 100 times less expensive 
-- ?
Somehow difficult to admit.

Regards,
Sergei



More information about the Glasgow-haskell-users mailing list