
17 Oct
2012
17 Oct
'12
11:17 a.m.
* Serge D. Mechveliani
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) Roman