BJG

BJG BJG

Niner since 2009

Comments

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 7 of 13

    I *think* Erik said that using the sum . map variant on length would be less efficient but it runs faster for me.  (using timing method from here).

     

    import Text.Printf import Control.Exception import System.CPUTime time :: IO t -> IO t time a = do start <- getCPUTime v <- a end <- getCPUTime let diff = (fromIntegral (end - start)) / (10^12) printf "Computation time: %0.3f sec\n" (diff
     :: Double) return v len1 = sum . map ( \ _ -> 1 ) len2 = foldr ( \ _ n -> n+1) 0 limit = 500000 main = do putStrLn "Len1:" time $ len1 [1..limit] `seq` return () putStrLn "Len2:" time $ len2 [1..limit] `seq` return ()

     

    I get

    *Main> :run main Len1: Computation time: 0.719 sec Len2: Computation time: 1.141 sec