<div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div class="h5">>>> During a small project I'm trying to develop a small application. It<br>
>>> becomes quite often that I need a function mapapp:<br>
>>><br>
>>> mapapp _ [] ap = ap<br>
>>> mapapp f (a:as) ap = f a : map f as ap<br></div></div></blockquote><div> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div class="h5">>> Of course,<br>
>>> (map f list) ++ append<br>
>>> would do the same as<br>
>>><br>
>>> mapapp f list append<br>
>>><br>
>>> but with less efficiency. Or am I wrong?<br>
</div></div></blockquote><div><br>I timed each of the following five operations with ...<br><br>> ghc -O2 --make MapApp.hs<br>> time ./MapApp<br><br>... and they produced no statistically significant differences. Each ran for about 3.8 seconds. Perhaps you can try it to convince yourself?<br>
<br>Sean<br><br>--<br><br>module Main where<br><br>mapapp0 :: (a -> b) -> [b] -> [a] -> [b]<br>mapapp0 f tail xs = map f xs ++ tail<br><br>mapapp1 :: (a -> b) -> [b] -> [a] -> [b]<br>mapapp1 _ tail [] = tail<br>
mapapp1 f tail (a:as) = f a : mapapp1 f tail as<br><br>mapapp2 :: (a -> b) -> [b] -> [a] -> [b]<br>mapapp2 f tail = go<br> where<br> go [] = tail<br> go (x:xs) = f x : go xs<br><br>mapapp3 :: (a -> b) -> [b] -> [a] -> [b]<br>
mapapp3 f tail = foldr ((:) . f) tail<br><br>main = do<br> writeFile "/dev/null" $ show $ [1 .. 10001000]<br> -- writeFile "/dev/null" $ show $ mapapp0 (+3) [1 .. 10000000] [1 .. 1000]<br> -- writeFile "/dev/null" $ show $ mapapp1 (+3) [1 .. 10000000] [1 .. 1000]<br>
-- writeFile "/dev/null" $ show $ mapapp2 (+3) [1 .. 10000000] [1 .. 1000]<br> -- writeFile "/dev/null" $ show $ mapapp3 (+3) [1 .. 10000000] [1 .. 1000]<br> return ()<br></div></div>