summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Breitner <mail@joachim-breitner.de>2016-08-05 21:39:07 -0400
committerJoachim Breitner <mail@joachim-breitner.de>2016-08-05 21:39:07 -0400
commit9aa5d87a28fbc328660829ec2e4841ea1a7a1440 (patch)
tree496cce750076c1aa6853e06fd0ee8b69c936bf0c
parent02614fd61f57b599c5e4fd5e85f00a4e1ce37bc7 (diff)
downloadhaskell-9aa5d87a28fbc328660829ec2e4841ea1a7a1440.tar.gz
Util.count: Implement as a left-fold instead of a right-fold
-rw-r--r--compiler/utils/Util.hs9
1 files changed, 5 insertions, 4 deletions
diff --git a/compiler/utils/Util.hs b/compiler/utils/Util.hs
index d20a604519..121fdbbf6f 100644
--- a/compiler/utils/Util.hs
+++ b/compiler/utils/Util.hs
@@ -1,6 +1,6 @@
-- (c) The University of Glasgow 2006
-{-# LANGUAGE CPP #-}
+{-# LANGUAGE CPP, BangPatterns #-}
-- | Highly random utility functions
--
@@ -619,9 +619,10 @@ all2 _ _ _ = False
-- Count the number of times a predicate is true
count :: (a -> Bool) -> [a] -> Int
-count _ [] = 0
-count p (x:xs) | p x = 1 + count p xs
- | otherwise = count p xs
+count p = go 0
+ where go !n [] = n
+ go !n (x:xs) | p x = go (n+1) xs
+ | otherwise = go n xs
{-
@splitAt@, @take@, and @drop@ but with length of another