Van der Corput sequence
First a cheap implementation in base 2, using string operations.
constant VdC = map { :2("0." ~ .base(2).flip) }, ^Inf;
.say for VdC[^16];Here is a more elaborate version using the polymod built-in integer method:
sub VdC($base = 2) {
map {
[+] $_ && .polymod($base xx *) Z/ [\*] $base xx *
}, ^Inf
}
.say for VdC[^10];Output:
0
0.5
0.25
0.75
0.125
0.625
0.375
0.875
0.0625
0.5625Here is a fairly standard imperative version in which we mutate three variables in parallel:
Output:
Here is a functional version that produces the same output:
We first define two sequences, one finite, one infinite.
When we zip those sequences together, the finite sequence terminates the loop (which, since a Raku loop returns all its values, is merely another way of writing a map).
We then sum with [+], a reduction of the + operator.
(We could have in-lined the sequences or used a traditional map operator, but this way seems more readable than the typical FP solution.)
The do is necessary to introduce a statement where a term is expected, since Raku distinguishes "sentences" from "noun phrases" as a natural language might.
Last updated
Was this helpful?