summaryrefslogtreecommitdiff
path: root/compiler/utils/Util.lhs
diff options
context:
space:
mode:
authorSimon Peyton Jones <simonpj@microsoft.com>2013-08-16 11:15:33 +0100
committerSimon Peyton Jones <simonpj@microsoft.com>2013-08-19 09:46:10 +0100
commit098c7d1786d58bb9d2a6e1297707489488588d75 (patch)
tree466e3dbaf5882de594bb9af1d94e6ba620ee8ea1 /compiler/utils/Util.lhs
parent83440fdc4f1b52b2f398326e0e86d4583afa433a (diff)
downloadhaskell-098c7d1786d58bb9d2a6e1297707489488588d75.tar.gz
Add a better implementation of dropTail, and use it
Diffstat (limited to 'compiler/utils/Util.lhs')
-rw-r--r--compiler/utils/Util.lhs10
1 files changed, 9 insertions, 1 deletions
diff --git a/compiler/utils/Util.lhs b/compiler/utils/Util.lhs
index dd947ffd93..5c82c757aa 100644
--- a/compiler/utils/Util.lhs
+++ b/compiler/utils/Util.lhs
@@ -574,7 +574,15 @@ splitAtList (_:xs) (y:ys) = (y:ys', ys'')
-- drop from the end of a list
dropTail :: Int -> [a] -> [a]
-dropTail n = reverse . drop n . reverse
+-- Specification: dropTail n = reverse . drop n . reverse
+-- Better implemention due to Joachim Breitner
+-- http://www.joachim-breitner.de/blog/archives/600-On-taking-the-last-n-elements-of-a-list.html
+dropTail n xs
+ = go (drop n xs) xs
+ where
+ go (_:ys) (x:xs) = x : go ys xs
+ go _ _ = [] -- Stop when ys runs out
+ -- It'll always run out before xs does
snocView :: [a] -> Maybe ([a],a)
-- Split off the last element