Rebol3 Code Examplex


Thue-Morse

Generate the binary sequence defined by the parity of 1-bits in integer indices (or by iterative complement-and-append).

Rebol [
    title: "Rosetta code: Thue-Morse"
    file:  %Thue-Morse.r3
    url:   https://rosettacode.org/wiki/Thue-Morse
]

thue-morse: function [
    "Generate the first max-steps sequences of the Thue-Morse construction"
    max-steps [integer!]
][
    result: copy [ ]
    val:    copy [0]
    count: 0
    forever [
        append result ajoin val               ;; stringify current sequence
        count: count + 1
        if count = max-steps [return result]
        append val map-each v val [1 - v]     ;; append the bit-flipped copy
    ]
]

foreach bits thue-morse 6 [print bits]

Output:

0
01
0110
01101001
0110100110010110
01101001100101101001011001101001