Population count
editable example
Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.
ghul
use IO.Std.write_line
use Ghul.Pipes
popcount(n: long) -> int => (
let bits mut = n
let count mut = 0
while bits != 0L do
count = count + cast int(bits & 1L)
bits = bits >>> 1
od
count
)
let powers_of_three = stream(1L, power => power || power * 3L)
let counts =
powers_of_three |> take(30) |> map(popcount) |> join(", ")
write_line("population counts of the first thirty powers of 3: {counts}")
let naturals = stream(0L, n => n || n + 1L)
let evil =
naturals |> filter(n => popcount(n) % 2 == 0) |> take(30) |> join(", ")
write_line("the first thirty evil numbers: {evil}")
let odious =
naturals |> filter(n => popcount(n) % 2 == 1) |> take(30) |> join(", ")
write_line("the first thirty odious numbers: {odious}")