Van der Corput sequence
First a cheap implementation in base 2, using string operations.
Here is a more elaborate version using the polymod built-in integer method:
Output:
Here 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