diff options
| author | Joachim Breitner <mail@joachim-breitner.de> | 2016-08-05 21:39:07 -0400 |
|---|---|---|
| committer | Joachim Breitner <mail@joachim-breitner.de> | 2016-08-05 21:39:07 -0400 |
| commit | 9aa5d87a28fbc328660829ec2e4841ea1a7a1440 (patch) | |
| tree | 496cce750076c1aa6853e06fd0ee8b69c936bf0c /compiler/utils | |
| parent | 02614fd61f57b599c5e4fd5e85f00a4e1ce37bc7 (diff) | |
| download | haskell-9aa5d87a28fbc328660829ec2e4841ea1a7a1440.tar.gz | |
Util.count: Implement as a left-fold instead of a right-fold
Diffstat (limited to 'compiler/utils')
| -rw-r--r-- | compiler/utils/Util.hs | 9 |
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 |
