summaryrefslogtreecommitdiff
path: root/libraries/base/Data/List.hs
diff options
context:
space:
mode:
authorDaniel Fischer <daniel.is.fischer@web.de>2010-10-02 23:12:01 +0000
committerDaniel Fischer <daniel.is.fischer@web.de>2010-10-02 23:12:01 +0000
commitefc5f12b7152154745e9edec6dd6aa76dd46a60b (patch)
treeed10c6f49312af725e6d6fd867f2b38fa4541f4c /libraries/base/Data/List.hs
parent13e2f4a98ca60e8e8bee678a38bb9f57d7bef156 (diff)
downloadhaskell-efc5f12b7152154745e9edec6dd6aa76dd46a60b.tar.gz
Lazier intersperse
A lazier implementation of intersperse, and consequentially intercalate, to avoid space leaks.
Diffstat (limited to 'libraries/base/Data/List.hs')
-rw-r--r--libraries/base/Data/List.hs12
1 files changed, 10 insertions, 2 deletions
diff --git a/libraries/base/Data/List.hs b/libraries/base/Data/List.hs
index c954757ddd..221a340a24 100644
--- a/libraries/base/Data/List.hs
+++ b/libraries/base/Data/List.hs
@@ -422,8 +422,16 @@ intersectBy eq xs ys = [x | x <- xs, any (eq x) ys]
intersperse :: a -> [a] -> [a]
intersperse _ [] = []
-intersperse _ [x] = [x]
-intersperse sep (x:xs) = x : sep : intersperse sep xs
+intersperse sep (x:xs) = x : prependToAll sep xs
+
+
+-- Not exported:
+-- We want to make every element in the 'intersperse'd list available
+-- as soon as possible to avoid space leaks. Experiments suggested that
+-- a separate top-level helper is more efficient than a local worker.
+prependToAll :: a -> [a] -> [a]
+prependToAll _ [] = []
+prependToAll sep (x:xs) = sep : x : prependToAll sep xs
-- | 'intercalate' @xs xss@ is equivalent to @('concat' ('intersperse' xs xss))@.
-- It inserts the list @xs@ in between the lists in @xss@ and concatenates the