[commit: ghc] master: Add a better implementation of dropTail, and use it (098c7d1)

git at git.haskell.org git at git.haskell.org
Mon Aug 19 10:50:11 CEST 2013


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/098c7d1786d58bb9d2a6e1297707489488588d75/ghc

>---------------------------------------------------------------

commit 098c7d1786d58bb9d2a6e1297707489488588d75
Author: Simon Peyton Jones <simonpj at microsoft.com>
Date:   Fri Aug 16 11:15:33 2013 +0100

    Add a better implementation of dropTail, and use it


>---------------------------------------------------------------

098c7d1786d58bb9d2a6e1297707489488588d75
 compiler/ghci/Linker.lhs |    2 +-
 compiler/utils/Util.lhs  |   10 +++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/compiler/ghci/Linker.lhs b/compiler/ghci/Linker.lhs
index 45bc9d5..192df2e 100644
--- a/compiler/ghci/Linker.lhs
+++ b/compiler/ghci/Linker.lhs
@@ -637,7 +637,7 @@ getLinkDeps hsc_env hpt pls replace_osuf span mods
 
             adjust_ul new_osuf (DotO file) = do
                 MASSERT(osuf `isSuffixOf` file)
-                let file_base = reverse (drop (length osuf + 1) (reverse file))
+                let file_base = dropTail (length osuf + 1) file
                     new_file = file_base <.> new_osuf
                 ok <- doesFileExist new_file
                 if (not ok)
diff --git a/compiler/utils/Util.lhs b/compiler/utils/Util.lhs
index dd947ff..5c82c75 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





More information about the ghc-commits mailing list