Task

Create a Thue-Morse sequence.



See also
   YouTube entry: The Fairest Sharing Sequence Ever
   YouTube entry: Math and OCD - My story with the Thue-Morse sequence
   Task: Fairshare between two and more


% Yields an infinite Thue-Morse sequence, digit by digit
tm = iter () yields (char)
    n: int := 1
    s: string := "0"
    while true do
        while n <= string$size(s) do
            yield(s[n])
            n := n + 1
        end
        s2: array[char] := array[char]$[]
        for c: char in string$chars(s) do
            if c='0' 
                then array[char]$addh(s2, '1')
                else array[char]$addh(s2, '0')
            end
        end
        s := s || string$ac2s(s2)
    end
end tm
 
% Print the first 64 characters
start_up = proc ()
    AMOUNT = 64
    po: stream := stream$primary_output()
    n: int := 0
    for c: char in tm() do
        stream$putc(po, c)
        n := n + 1
        if n = AMOUNT then break end
    end
end start_up