
On Wednesday 26 May 2010 5:38:57 pm Pete Chown wrote:
test :: (Eq a) => (Int -> a) -> (Int -> a) -> Bool test f1 f2 = unsafePerformIO $ do goodSoFar <- newIORef True forLoop 1 100 $ \i -> when (f1 i /= f2 i) $ writeIORef goodSoFar False readIORef goodSoFar
The problem with this algorithm is that it needlessly tests f1 against f2 for all i, even when a non-matching value has has already been found. Using the power of call-with-current-continuation, I have constructed an algorithm that lacks this deficiency: import Control.Monad.Cont test f g = flip runCont id . callCC $ \escape -> do forM_ [1..100] $ \n -> when (f n /= g n) $ escape False return True This should perform almost 75% less work in the testFunc1 case! It certainly feels much snappier. -- Dan