[Haskell-cafe] Re: Math questions

Pete Chown 1 at 234.cx
Wed May 26 17:38:57 EDT 2010


Mujtaba Boori wrote:

> Define a higher order function  that tests whether two functions , both
> defined on integers , coincide for all integers between 1 and 100

If this really is a homework question, I dare you to submit this 
solution.  Try it for yourself, it works fine. :-)

module Main where

import Control.Monad
import Data.IORef
import System.IO.Unsafe

forLoop :: Int -> Int -> (Int -> IO ()) -> IO ()
forLoop start stop body = mapM_ body [start..stop]

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

testFunc1 27 = "numpty"
testFunc1 i = show i

testFunc2 270 = "numpty"
testFunc2 i = show i

main = do
   print $ test show testFunc1
   print $ test show testFunc2



More information about the Haskell-Cafe mailing list