enumFromThenTo for Doubles

Andrew Farmer xichekolas at gmail.com
Wed Aug 10 03:22:07 UTC 2016

Noticed this today:

ghci> let xs = [0.0,0.1 .. 86400.0] in maximum xs

enumFromThenTo is implemented by numericEnumFromThenTo:


Which probably accumulates error in numericEnumFromThen with the (m+m-n):

numericEnumFromThen n m = n `seq` m `seq` (n : numericEnumFromThen m (m+m-n))

Why not define numericEnumFromThen as:

numericEnumFromThen n m = let d = m - n in d `seq` go d n
where go delta x = x `seq` (x : go delta (x + delta))

(or with BangPatterns)

numericEnumFromThen n m = go (m - n) n
where go !delta !x = x : go delta (x + delta)

Seems like we'd save a lot of subtractions by using the worker function.

More information about the ghc-devs mailing list