Population count: Difference between revisions

m
→‎{{header|Haskell}}: Tidied, applied Ormolu.
(Add BCPL)
m (→‎{{header|Haskell}}: Tidied, applied Ormolu.)
Line 2,029:
Or, if we want to write our own popCount, perhaps something like:
 
<lang haskell>import Data.ListBifoldable (partition, unfoldrbiList)
import Data.Bifoldable (biList)
import Data.Tuple (swap)
import Data.Bool (bool)
import Data.List (partition, unfoldr)
import Data.BifoldableTuple (biListswap)
 
-- POPCOUNT ------------------------------------------ POPULATION COUNT -------------------
popCount :: Int -> Int
popCount = sum . unfoldr go
where
sum . unfoldr ((bool Nothing . Just . swap . flip quotRem 2) <*> (0 <))
go =
( bool Nothing
. Just
. swap
. flip quotRem 2
)
<*> (0 <)
 
-- TEST ---------------------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
zipWith
(\k xs -> kconcat ++[k, ":\n" ++, show xs ++, "\n"])
["Population count of powers of 3", "evil", "odious"]
( (popCount . (3 ^) <$> [0 .. 29]) :
biList (partition (even . popCount) [0 .. 59]))</lang>
)</lang>
{{Out}}
<pre>Population count of powers of 3:
9,655

edits