Population count: Difference between revisions
m
→{{header|Haskell}}: Tidied, applied Ormolu.
Not a robot (talk | contribs) (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.
import Data.Bifoldable (biList)▼
import Data.Bool (bool)
import Data.List (partition, unfoldr)
popCount :: Int -> Int
popCount = sum . unfoldr go
where
go =
( bool Nothing
. Just
. swap
. flip quotRem 2
)
<*> (0 <)
main :: IO ()
main =
mapM_ putStrLn $
zipWith
(\k xs ->
["Population count of powers of 3", "evil", "odious"]
( (popCount . (3 ^) <$> [0 .. 29]) :
biList (partition (even . popCount) [0 .. 59])
)</lang>
{{Out}}
<pre>Population count of powers of 3:
|