Thue-Morse: Difference between revisions

14,900 bytes added ,  1 month ago
 
(36 intermediate revisions by 16 users not shown)
Line 14:
{{trans|Python: By counting set ones in binary representation}}
 
<langsyntaxhighlight lang="11l">F thue_morse_digits(digits)
R (0 .< digits).map(n -> binbits:popcount(n).count(‘1’) % 2)
 
print(thue_morse_digits(20))</langsyntaxhighlight>
 
{{out}}
Line 41:
after all, XOR is commutative.
 
<langsyntaxhighlight lang="8080asm"> org 100h
;;; Write 256 bytes of ASCII '0' starting at address 200h
lxi h,200h ; The array is page-aligned so L starts at 0
Line 58:
mvi c,9 ; Syscall 9 = print string
lxi d,200h ; The string is at 200h
jmp 5</langsyntaxhighlight>
 
{{out}}
Line 68:
1001011001101001011010011001011001101001100101101001011001101001
0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Next(CHAR ARRAY s)
BYTE i,len
CHAR c
 
IF s(0)=0 THEN
s(0)=1 s(1)='0
RETURN
FI
 
FOR i=1 TO s(0)
DO
IF s(i)='0 THEN
c='1
ELSE
c='0
FI
s(s(0)+i)=c
OD
s(0)==*2
RETURN
 
PROC Main()
BYTE i
CHAR ARRAY s(256)
 
s(0)=0
FOR i=0 TO 7
DO
Next(s)
PrintF("T%B=%S%E%E",i,s)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Thue-Morse.png Screenshot from Atari 8-bit computer]
<pre>
T0=0
 
T1=01
 
T2=0110
 
T3=01101001
 
T4=0110100110010110
 
T5=01101001100101101001011001101001
 
T6=0110100110010110100101100110100110010110011010010110100110010110
 
T7=01101001100101101001011001101001100101100110100101101001100101101001011001101001011010011001011001101001100101101001011001101001
</pre>
 
=={{header|Ada}}==
Implementation using an L-system.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Thue_Morse is
Line 89 ⟶ 142:
Ada.Text_IO.Put_Line(Integer'Image(I) & ": " & Sequence(I));
end loop;
end Thue_Morse;</langsyntaxhighlight>
 
{{out}}
Line 101 ⟶ 154:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># "flips" the "bits" in a string (assumed to contain only "0" and "1" characters) #
OP FLIP = ( STRING s )STRING:
BEGIN
Line 116 ⟶ 169:
print( ( tm, newline ) );
tm +:= FLIP tm
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 127 ⟶ 180:
0110100110010110100101100110100110010110011010010110100110010110
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">⍝ generate the first ⍵ elements of the Thue-Morse sequence
tm←{⍵⍴(⊢,~)⍣(⍵≤(⍴⊢))⊢,0}</syntaxhighlight>
{{out}}
<pre> tm 32
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1</pre>
 
=={{header|AppleScript}}==
Line 133 ⟶ 193:
{{Trans|JavaScript}}
 
<langsyntaxhighlight AppleScriptlang="applescript">------------------------ THUE MORSE ----------------------
 
-- thueMorse :: Int -> String
Line 225 ⟶ 285:
end script
end if
end mReturn</langsyntaxhighlight>
<pre>"0110100110010110100101100110100110010110011010010110100110010110"</pre>
----
Line 233 ⟶ 293:
Implements the "flip previous cycles" method, stopping at a specified sequence length.
 
<langsyntaxhighlight lang="applescript">on ThueMorse(sequenceLength)
if (sequenceLength < 1) then return ""
Line 262 ⟶ 322:
end ThueMorse
 
return linefeed & ThueMorse(64) & (linefeed & ThueMorse(65))</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"
0110100110010110100101100110100110010110011010010110100110010110
01101001100101101001011001101001100101100110100101101001100101101"</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">thueMorse: function [maxSteps][
result: new []
val: [0]
Line 284 ⟶ 344:
]
loop thueMorse 6 'bits ->
print bits</langsyntaxhighlight>
 
{{out}}
Line 296 ⟶ 356:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">ThueMorse(num, iter){
if !iter
return num
Line 303 ⟶ 363:
res := ThueMorse(num . opp, --iter)
return res
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">num := 0
loop 7
output .= A_Index-1 " : " ThueMorse(num, A_Index-1) "`n"
MsgBox % output</langsyntaxhighlight>
{{out}}
<pre>0 : 0
Line 319 ⟶ 379:
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">BEGIN{print x="0"}
{gsub(/./," &",x);gsub(/ 0/,"01",x);gsub(/ 1/,"10",x);print x}</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
tm = "0"
 
Line 346 ⟶ 406:
Next j
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 353 ⟶ 413:
 
==={{header|Sinclair ZX81 BASIC}}===
<langsyntaxhighlight lang="basic"> 10 LET T$="0"
20 PRINT "T0=";T$
30 FOR I=1 TO 7
Line 364 ⟶ 424:
100 NEXT J
110 PRINT T$
120 NEXT I</langsyntaxhighlight>
{{out}}
<pre>T0=0
Line 376 ⟶ 436:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic">REM >thuemorse
tm$ = "0"
PRINT tm$
Line 391 ⟶ 451:
IF MID$(previous$, i%, 1) = "1" THEN tm$ += "0" ELSE tm$ += "1"
NEXT
= previous$ + tm$</langsyntaxhighlight>
{{out}}
<pre>0
Line 404 ⟶ 464:
 
=={{header|BCPL}}==
<langsyntaxhighlight BCPLlang="bcpl">get "libhdr"
 
let parity(x) =
Line 413 ⟶ 473:
$( for i=0 to 63 do writen(parity(i))
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
Line 422 ⟶ 482:
This implements the algorithm that counts the 1 bits in the binary representation of the sequence number.
 
<langsyntaxhighlight lang="befunge">:0\:!v!:\+g20\<>*:*-!#@_
86%2$_:2%02p2/^^82:+1,+*</langsyntaxhighlight>
 
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110100110010110100101100110100110010110011010010110100110010110011010011001011010010110011010010110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|Binary Lambda Calculus}}==
 
The (infinite) Thue-Morse sequence is output by the 115 bit BLC program
 
<pre>0001000110100001010100011010000000000101101110000101100000010111111101011001111001111110111110000011001011010000010</pre>
 
as documented in https://github.com/tromp/AIT/blob/master/characteristic_sequences/thue-morse.lam
 
Output:
 
<pre>01101001100101101001011001101001100101100110100101101001100101101001011001101001011010011001011001101001100101101001011001101001100101100110100101101001100101100110100110010110100101100110100101101001...</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">TM ← {𝕩↑(⊢∾¬)⍟(1+⌈2⋆⁼𝕩)⥊0}
 
TM 25 #get first 25 elements</syntaxhighlight>
{{out}}
<pre>⟨ 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 ⟩</pre>
 
=={{header|C}}==
===C: Using string operations===
{{trans|Java}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 449 ⟶ 528:
puts(sequence);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 457 ⟶ 536:
 
===C: By counting ones in binary representation of an iterator===
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
/**
Line 481 ⟶ 560:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 490 ⟶ 569:
 
===C: By counting ones in binary representation of an iterator (w/User options)===
<langsyntaxhighlight Clang="c"> #include <stdio.h>
 
/**
Line 538 ⟶ 617:
 
return 0;
} </langsyntaxhighlight>
 
{{out}}
Line 549 ⟶ 628:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Text;
 
Line 575 ⟶ 654:
}
}
}</langsyntaxhighlight>
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <iterator>
Line 597 ⟶ 676:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 608 ⟶ 687:
0110100110010110100101100110100110010110011010010110100110010110
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% 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</syntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. THUE-MORSE.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 STRINGS.
03 CURRENT-STATE PIC X(64).
03 TEMP PIC X(64).
 
PROCEDURE DIVISION.
BEGIN.
MOVE "0" TO CURRENT-STATE.
PERFORM THUE-MORSE-STEP 8 TIMES.
DISPLAY CURRENT-STATE.
STOP RUN.
 
THUE-MORSE-STEP.
MOVE CURRENT-STATE TO TEMP.
INSPECT TEMP REPLACING ALL '0' BY 'X'.
INSPECT TEMP REPLACING ALL '1' BY '0'.
INSPECT TEMP REPLACING ALL 'X' BY '1'.
STRING CURRENT-STATE DELIMITED BY SPACE,
TEMP DELIMITED BY SPACE
INTO CURRENT-STATE.</syntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun bit-complement (bit-vector)
(loop with result = (make-array (length bit-vector) :element-type 'bit)
for b across bit-vector
Line 630 ⟶ 772:
do (print-bit-vector value)))
 
(thue-morse 6)</langsyntaxhighlight>
{{out}}
<pre>
Line 641 ⟶ 783:
0110100110010110100101100110100110010110011010010110100110010110
</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Find the N'th digit in the Thue-Morse sequence
sub tm(n: uint32): (d: uint8) is
var n2 := n;
while n2 != 0 loop
n2 := n2 >> 1;
n := n ^ n2;
end loop;
d := (n & 1) as uint8;
end sub;
 
# Print the first 64 digits
var i: uint32 := 0;
while i < 64 loop
print_char('0' + tm(i));
i := i + 1;
end loop;
print_nl();</syntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|Crystal}}==
{{trans|Javascript}}
<langsyntaxhighlight lang="ruby">steps = 6
 
tmp = ""
Line 656 ⟶ 821:
}
 
puts s1</langsyntaxhighlight>
 
{{out}}
Line 665 ⟶ 830:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.range;
import std.stdio;
 
Line 693 ⟶ 858:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 707 ⟶ 872:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Thue-Morse#Pascal Pascal].
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* Find the N'th digit in the Thue-Morse sequence */
proc nonrec tm(word n) byte:
word n2;
n2 := n;
while n2 ~= 0 do
n2 := n2 >> 1;
n := n >< n2
od;
n & 1
corp
 
/* Print the first 64 digits */
proc nonrec main() void:
byte i;
for i from 0 upto 63 do
write(tm(i):1)
od
corp</syntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
func$ tmorse s$ .
for i to len s$
if substr s$ i 1 = "1"
k$ &= "0"
else
k$ &= "1"
.
.
return s$ & k$
.
tm$ = "0"
print tm$
for j to 7
tm$ = tmorse tm$
print tm$
.
</syntaxhighlight>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 46.x :
<langsyntaxhighlight lang="elena">import extensions;
import system'text;
 
Line 718 ⟶ 926:
var sb1 := TextBuilder.load("0");
var sb2 := TextBuilder.load("1");
for(int i := 0,; i < steps,; i += 1)
{
var tmp := sb1.Value;
Line 730 ⟶ 938:
{
sequence(6)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 737 ⟶ 945:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">Enum.reduce(0..6, '0', fn _,s ->
IO.puts s
s ++ Enum.map(s, fn c -> if c==?0, do: ?1, else: ?0 end)
Line 745 ⟶ 953:
Stream.iterate('0', fn s -> s ++ Enum.map(s, fn c -> if c==?0, do: ?1, else: ?0 end) end)
|> Enum.take(7)
|> Enum.each(&IO.puts/1)</langsyntaxhighlight>
 
{{out}}
Line 766 ⟶ 974:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">thueMorse
=LAMBDA(n,
APPLYN(n)(
Line 777 ⟶ 985:
)
)(0)
)</langsyntaxhighlight>
 
and also assuming the following generic bindings in the Name Manager for the WorkBook:
 
<langsyntaxhighlight lang="lisp">APPLYN
=LAMBDA(n,
LAMBDA(f,
Line 813 ⟶ 1,021:
)
)
)</langsyntaxhighlight>
 
{{Out}}
Line 972 ⟶ 1,180:
| 0110100110010110100101100110100110010110011010010110100110010110
|}
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Thue-Morse. Nigel Galloway: April 16th., 2024
let rec fG n g=match n with 0->g |1->g+1 |n ->fG(n/2)(g+n&&&1)
let thueMorse=Seq.initInfinite(fun n->(fG n 0)%2)
thueMorse|>Seq.take 25|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<langsyntaxhighlight lang="factor">USING: io kernel math math.parser sequences ;
 
: thue-morse ( seq n -- seq' )
Line 982 ⟶ 1,202:
: print-tm ( seq -- ) [ number>string ] map "" join print ;
 
7 <iota> [ { 0 } swap thue-morse print-tm ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 996 ⟶ 1,216:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program thue_morse
implicit none
logical :: f(32) = .false.
Line 1,008 ⟶ 1,228:
end do
end program thue_morse</langsyntaxhighlight>
{{out}}
<pre>
Line 1,020 ⟶ 1,240:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Dim As String tm = "0"
 
Line 1,041 ⟶ 1,261:
Next j
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,053 ⟶ 1,273:
01101001100101101001011001101001100101100110100101101001100101101001011001101001011010011001011001101001100101101001011001101001
</pre>
 
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn ThueMorse( s as Str255 ) as Str255
Str255 k
short i
k = ""
for i = 1 to len$(s)
if mid$(s, i, 1) == "1"
k += "0"
else
k += "1"
end if
next
end fn = s + k
 
local fn DoIt
Str255 tm
short i
tm = "0"
print tm
for i = 1 to 7
tm = fn ThueMorse( tm )
print tm
next
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
0
01
0110
01101001
0110100110010110
01101001100101101001011001101001
0110100110010110100101100110100110010110011010010110100110010110
01101001100101101001011001101001100101100110100101101001100101101001011001101001011010011001011001101001100101101001011001101001
</pre>
 
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Thue-Morse}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
=== Solution 1 ===
 
[[File:Fōrmulæ - Thue–Morse sequence 01.png]]
 
[[File:Fōrmulæ - Thue–Morse sequence 11.png]]
 
=== Solution 2 ===
 
[[File:Fōrmulæ - Thue–Morse sequence 02.png]]
 
[[File:Fōrmulæ - Thue–Morse sequence 11.png]]
 
=== Solution 3 ===
 
[[File:Fōrmulæ - Thue–Morse sequence 03.png]]
 
[[File:Fōrmulæ - Thue–Morse sequence 11.png]]
 
=== Solution 4 ===
 
Notice that this solution generates a string.
 
[[File:Fōrmulæ - Thue–Morse sequence 04.png]]
 
[[File:Fōrmulæ - Thue–Morse sequence 12.png]]
 
=== Solution 5 ===
 
Notice that this solution generates a string.
 
[[File:Fōrmulæ - Thue–Morse sequence 05.png]]
 
[[File:Fōrmulæ - Thue–Morse sequence 12.png]]
 
=== Solution 6 ===
 
Notice that this solution generates a string.
 
[[File:Fōrmulæ - Thue–Morse sequence 06.png]]
 
[[File:Fōrmulæ - Thue–Morse sequence 12.png]]
 
=== Solution 7. L-system ===
 
There are generic functions written in Fōrmulæ to compute an L-system in the page [[L-system#Fōrmulæ | L-system]].
 
The program that creates a Hilbert curve is:
 
[[File:Fōrmulæ - L-system - Thue-Morse 01.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Thue–Morse sequence 12.png]]
In '''[https://formulae.org/?example=Thue-Morse this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">// prints the first few members of the Thue-Morse sequence
 
package main
Line 1,094 ⟶ 1,409:
fmt.Println( tmBuffer.String() )
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,110 ⟶ 1,425:
Computing progressively longer prefixes of the sequence:
 
<langsyntaxhighlight lang="haskell">thueMorsePxs :: [[Int]]
thueMorsePxs = iterate ((++) <*> map (1 -)) [0]
 
Line 1,119 ⟶ 1,434:
-}
main :: IO ()
main = print $ thueMorsePxs !! 5</langsyntaxhighlight>
{{Out}}
<pre>[0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1]</pre>
Line 1,125 ⟶ 1,440:
The infinite sequence itself:
 
<langsyntaxhighlight lang="haskell">thueMorse :: [Int]
thueMorse = 0 : g 1
where
Line 1,131 ⟶ 1,446:
main :: IO ()
main = print $ take 33 thueMorse</langsyntaxhighlight>
{{Out}}
<pre>[0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1]</pre>
Line 1,139 ⟶ 1,454:
We only show a prefix of the sequence:
 
<langsyntaxhighlight Jlang="j"> (, -.)@]^:[&0]9
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 ...</langsyntaxhighlight>
 
Or, more compactly:
 
<langsyntaxhighlight Jlang="j"> ' '-.~":(, -.)@]^:[&0]9
0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110100110010110100101100110100110010110011010010110100110010110011010011001011010010110011010010110100110010110100101100110100110010110011010010110100110010110...</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class ThueMorse {
 
public static void main(String[] args) {
Line 1,164 ⟶ 1,479:
System.out.println(sb1);
}
}</langsyntaxhighlight>
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
Line 1,170 ⟶ 1,485:
===ES5===
{{trans|Java}}
<langsyntaxhighlight JavaScriptlang="javascript">(function(steps) {
'use strict';
var i, tmp, s1 = '0', s2 = '1';
Line 1,179 ⟶ 1,494:
}
console.log(s1);
})(6);</langsyntaxhighlight>
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,272 ⟶ 1,587:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1]</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
`thueMorse` as defined here generates an indefinitely long stream of the Thue-Morse integers:
<syntaxhighlight lang="text">def thueMorse:
0,
({sb0: "0", sb1: "1", n:1 }
| while( true;
{n: (.sb0|length),
sb0: (.sb0 + .sb1),
sb1: (.sb1 + .sb0)} )
| .sb0[.n:]
| explode[]
| . - 48);</syntaxhighlight>
'''Example:'''
<syntaxhighlight lang="text">[limit(100;thueMorse)] | join("")</syntaxhighlight>
{{out}}
<pre> 0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function thuemorse(len::Int)
rst = Vector{Int8}(len)
rst[1] = 0
Line 1,293 ⟶ 1,629:
end
 
println(join(thuemorse(100)))</langsyntaxhighlight>
 
{{out}}
Line 1,302 ⟶ 1,638:
{{trans|Java}}
The original Java code, as translated to Kotlin, with a few cleanups.
<langsyntaxhighlight lang="kotlin">fun thueMorse(n: Int): String {
val sb0 = StringBuilder("0")
val sb1 = StringBuilder("1")
Line 1,315 ⟶ 1,651:
fun main() {
for (i in 0..6) println("$i : ${thueMorse(i)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,330 ⟶ 1,666:
===Kotlin: Alternative===
Same general idea as above, but using a few Kotlin specific code shortcuts.
<langsyntaxhighlight lang="kotlin">fun thueMorse(n: Int): String {
val pair = "0" to "1"
repeat(n) { pair = with(pair) { first + second to second + first } }
Line 1,338 ⟶ 1,674:
fun main() {
for (i in 0..6) println("$i : ${thueMorse(i)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,352 ⟶ 1,688:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
 
{def thue_morse
Line 1,367 ⟶ 1,703:
-> 0110100110010110100101100110100110010110011010010110100110010110
 
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">ThueMorse = {sequence = "0"}
 
function ThueMorse:show ()
Line 1,391 ⟶ 1,727:
ThueMorse:show()
ThueMorse:addBlock()
end</langsyntaxhighlight>
{{out}}
<pre>0
Line 1,398 ⟶ 1,734:
01101001
0110100110010110</pre>
=={{header|M2000 Interpreter}}==
Adapted from Java.
===One by One===
The thuemorse lambda function return another lambda, which used to send specific part of message, until end of message (return empty string).
 
<syntaxhighlight lang="m2000 interpreter">
thuemorse$=lambda$ (n as integer)->{
def sb0$="0", sb1$="1"
n=max.data(0, n)
=lambda$
sb0$, sb1$,
n, park$
(many)->{
if n<0 and park$="" then exit
while n>0
tmp$=sb0$
sb0$=sb1$
sb1$=tmp$
n--
end while
if n>=0 then n-- :park$+=sb0$
 
if many<len(park$) then
=left$(park$, many)
park$=mid$(park$, many+1)
else
=park$:park$=""
end if
}
}
document log$
For i=0 to 6
log$="Message :"+str$(i,0)+{
}
t$=thuemorse$(i)
do
resp$=t$(16)
if resp$<>"" then
log$=resp$+"...transmitted"+{
}
else
exit
end if
always
next i
Clipboard log$
Report log$
</syntaxhighlight>
{{out}}
<pre>
Message :0
0...transmitted
Message :1
01...transmitted
Message :2
0110...transmitted
Message :3
01101001...transmitted
Message :4
0110100110010110...transmitted
Message :5
0110100110010110...transmitted
1001011001101001...transmitted
Message :6
0110100110010110...transmitted
1001011001101001...transmitted
1001011001101001...transmitted
0110100110010110...transmitted
</pre>
 
===All together===
 
<syntaxhighlight lang="m2000 interpreter">
// copy thuemorse lambda here//
dim t$(0 to 6)
document log$
jobs=stack
For i=6 to 0
t$(i)=thuemorse$(i)
stack jobs {push i}
next i
stack jobs {
while not empty
read i
resp$=t$(i)(16)
if resp$<>"" then
log$="Message :"+str$(i,0)+{
}
log$=resp$+"...transmitted"+{
}
data i
end if
end while
}
Clipboard log$
Report log$
</syntaxhighlight>
{{out}}
<pre>
Message :0
0...transmitted
Message :1
01...transmitted
Message :2
0110...transmitted
Message :3
01101001...transmitted
Message :4
0110100110010110...transmitted
Message :5
0110100110010110...transmitted
Message :6
0110100110010110...transmitted
Message :5
1001011001101001...transmitted
Message :6
1001011001101001...transmitted
Message :6
1001011001101001...transmitted
Message :6
0110100110010110...transmitted
</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE THUE
.MCALL .TTYOUT,.EXIT
THUE:: MOV #100,R3 ; 64 ITEMS
CLR R2
1$: JSR PC,SEQ ; GET THUE-MORSE SEQUENCE ITEM
ADD #60,R0 ; MAKE ASCII
.TTYOUT ; PRINT
INC R2
SOB R3,1$
.EXIT
 
; LET R0 = R2'TH ITEM IN THUE MORSE SEQUENCE
SEQ: CLR R0
MOV #20,R1
1$: ROR R0
XOR R2,R0
SOB R1,1$
BIC #^C1,R0
RTS PC
.END THUE</syntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">ThueMorse[Range[20]]</langsyntaxhighlight>
{{out}}
<pre>{1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0}</pre>
 
=={{header|MATLAB}}==
===MATLAB: By counting set ones in binary representation===
<syntaxhighlight lang="MATLAB">
tmSequence = thue_morse_digits(20);
disp(tmSequence);
 
function tmSequence = thue_morse_digits(n)
tmSequence = zeros(1, n);
for i = 0:(n-1)
binStr = dec2bin(i);
numOnes = sum(binStr == '1');
tmSequence(i+1) = mod(numOnes, 2);
end
end
</syntaxhighlight>
{{out}}
<pre>
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1
</pre>
 
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (show (take 30 thue) ++ "\n")]
 
thue :: [num]
thue = 0 : 1 : concat [[x, 1-x] | x<-tl thue]</syntaxhighlight>
{{out}}
<pre>[0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0]</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE ThueMorse;
FROM Strings IMPORT Concat;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,430 ⟶ 1,943:
Sequence(6);
ReadChar;
END ThueMorse.</langsyntaxhighlight>
 
=={{header|NewLISP}}==
 
<langsyntaxhighlight lang="newlisp">(define (Thue-Morse loops)
(setf TM '(0))
(println TM)
Line 1,449 ⟶ 1,962:
(Thue-Morse 5)
(exit)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,464 ⟶ 1,977:
 
===Using an iterator and sequences concatenations===
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
iterator thueMorse(maxSteps = int.high): string =
Line 1,476 ⟶ 1,989:
 
for bits in thueMorse(6):
echo bits</langsyntaxhighlight>
 
{{out}}
Line 1,487 ⟶ 2,000:
 
===Using fast sequence generation algorithm from Wikipedia===
<langsyntaxhighlight Nimlang="nim">type Bit = 0..1
 
proc thueMorse(seqLength: Positive): string =
Line 1,497 ⟶ 2,010:
result.add chr(val + ord('0'))
 
echo thueMorse(64)</langsyntaxhighlight>
 
{{out}}
Line 1,503 ⟶ 2,016:
 
=={{header|OASYS Assembler}}==
<langsyntaxhighlight lang="oasys_oaa">; Thue-Morse sequence
 
[*'A] ; Ensure the vocabulary is not empty
Line 1,520 ⟶ 2,033:
%@FO> ; Reset object pointer
CR ; Line break
| ; Repeat loop</langsyntaxhighlight>
The standard DOS-based interpreter will display an error message about word too long after 7 lines are output; this is because the 8th line does not fit in 80 columns.
 
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang="objeck">class ThueMorse {
function : Main(args : String[]) ~ Nil {
Sequence(6);
Line 1,541 ⟶ 2,054:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 1,551 ⟶ 2,064:
===By counting ones in binary representation of an iterator===
{{trans|C}}
<langsyntaxhighlight lang="ocaml">(* description: Counts the number of bits set to 1
input: the number to have its bit counted
output: the number of bits set to 1 *)
Line 1,569 ⟶ 2,082:
| _ -> assert false)
done;
print_newline ()</langsyntaxhighlight>
 
 
===Using string operations===
{{trans|Objeck}}
<langsyntaxhighlight lang="ocaml">let sequence steps =
let sb1 = Buffer.create 100 in
let sb2 = Buffer.create 100 in
Line 1,587 ⟶ 2,100:
 
let () =
print_endline (sequence 6);</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 1,593 ⟶ 2,106:
{{works with|Delphi}}
Like the C++ Version [[http://rosettacode.org/wiki/Thue-Morse#C.2B.2B]] the lenght of the sequence is given in advance.
<langsyntaxhighlight lang="pascal">Program ThueMorse;
function fThueMorse(maxLen: NativeInt):AnsiString;
Line 1,651 ⟶ 2,164:
fThueMorse(1 shl 30);
{$IFNDEF LINUX}readln;{$ENDIF}
end.</langsyntaxhighlight>
{{Output}}<pre>Compile with /usr/lib/fpc/3.0.1/ppc386 "ThueMorse.pas" -al -XX -Xs -O4 -MDelphi
without -O4 -> 2 secs
Line 1,668 ⟶ 2,181:
=={{header|Perl}}==
{{works with|Perl|5.x}}
<langsyntaxhighlight lang="perl">sub complement
{
my $s = shift;
Line 1,683 ⟶ 2,196:
$str .= complement($str);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,696 ⟶ 2,209:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">string</span> <span style="color: #000000;">tm</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
Line 1,702 ⟶ 2,215:
<span style="color: #000000;">tm</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tm</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{Out}}
<pre>
Line 1,716 ⟶ 2,229:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">def inverte
dup len
for
Line 1,729 ⟶ 2,242:
dup print nl nl
inverte chain
endfor</langsyntaxhighlight>
 
=={{header|PHP}}==
Line 1,737 ⟶ 2,250:
(see https://en.wikipedia.org/wiki/Thue%E2%80%93Morse_sequence)
 
<langsyntaxhighlight PHPlang="php"><?php
 
function thueMorseSequence($length) {
Line 1,753 ⟶ 2,266:
for ($n = 10 ; $n <= 100 ; $n += 10) {
echo sprintf('%3d', $n), ' : ', thueMorseSequence($n), PHP_EOL;
}</langsyntaxhighlight>
 
{{out}}
Line 1,768 ⟶ 2,281:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let R 0
(prinl R)
(for (X 1 (>= 32 X))
Line 1,777 ⟶ 2,290:
(bin (x| (dec (** 2 X)) R)) ) ) )
(prinl (pack 0 (bin R)))
(inc 'X X) ) )</langsyntaxhighlight>
 
=={{header|PL/M}}==
<syntaxhighlight lang="PLM">100H:
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
PUT$CHAR: PROCEDURE (C); DECLARE C BYTE; CALL BDOS(2,C); END PUT$CHAR;
 
/* FIND THE NTH ELEMENT OF THE THUE-MORSE SEQUENCE */
THUE: PROCEDURE (N) BYTE;
DECLARE N ADDRESS;
N = N XOR SHR(N,8);
N = N XOR SHR(N,4);
N = N XOR SHR(N,2);
N = N XOR SHR(N,1);
RETURN N AND 1;
END THUE;
 
/* PRINT THE FIRST 64 ELEMENTS */
DECLARE I BYTE;
DO I=0 TO 63;
CALL PUT$CHAR('0' + THUE(I));
END;
 
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">function New-ThueMorse ( $Digits )
{
# Start with seed 0
Line 1,810 ⟶ 2,350:
New-ThueMorse 5
New-ThueMorse 16
New-ThueMorse 73</langsyntaxhighlight>
{{out}}
<pre>01101
Line 1,818 ⟶ 2,358:
=={{header|PureBasic}}==
{{trans|C}}
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
 
Procedure.i count_bits(v.i)
Line 1,835 ⟶ 2,375:
Next
PrintN(~"\n...fin") : Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110100110010110100101100110100110010110011010010110100110010110011010011001011010010110011010010110100110010110100101100110100110010110011010010110100110010110
Line 1,842 ⟶ 2,382:
=={{header|Python}}==
===Python: By substitution===
<syntaxhighlight lang="python">
<lang Python>
m='0'
print(m)
Line 1,852 ⟶ 2,392:
m=m0+m
print(m)
</syntaxhighlight>
</lang>
{{out}}
<pre>0
Line 1,863 ⟶ 2,403:
 
===Python: By counting set ones in binary representation===
<syntaxhighlight lang="python">
<lang Python>
>>> def thue_morse_digits(digits):
... return [bin(n).count('1') % 2 for n in range(digits)]
Line 1,871 ⟶ 2,411:
 
>>>
</syntaxhighlight>
</lang>
 
===Python: By [http://mathworld.wolfram.com/SubstitutionSystem.html substitution system]===
<syntaxhighlight lang="python">
<lang Python>
>>> def thue_morse_subs(chars):
... ans = '0'
Line 1,884 ⟶ 2,424:
'01101001100101101001'
>>>
</syntaxhighlight>
</lang>
 
===Python: By pair-wise concatenation===
<syntaxhighlight lang="python">
<lang Python>
>>> def thue_morse(n):
... (v, i) = ('0', '1')
Line 1,897 ⟶ 2,437:
'0110100110010110100101100110100110010110011010010110100110010110'
>>>
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
This uses the [https://en.wikipedia.org/wiki/Thue%E2%80%93Morse_sequence#Fast_sequence_generation fast sequence generation algorithm] from the wiki article.
<langsyntaxhighlight lang="quackery">[ [] 0 rot times
[ i^ dup 1 - ^
dup 1 >> ^ hex 55555555 & if
Line 1,908 ⟶ 2,448:
[ digit join ] ] drop ] is thue-morse ( n --> $ )
 
20 thue-morse echo$ cr</langsyntaxhighlight>
{{out}}
<pre>
Line 1,915 ⟶ 2,455:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
thue_morse <- function(steps) {
sb1 <- "0"
Line 1,927 ⟶ 2,467:
}
cat(thue_morse(6), "\n")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,934 ⟶ 2,474:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(define 1<->0 (match-lambda [#\0 #\1] [#\1 #\0]))
(define (thue-morse-step (s "0"))
Line 1,943 ⟶ 2,483:
(if (zero? n) (reverse rv) (inr (sub1 n) (cons (thue-morse-step (car rv)) rv)))))
 
(for-each displayln (thue-morse 7))</langsyntaxhighlight>
{{out}}
<pre>0
Line 1,957 ⟶ 2,497:
{{Works with|rakudo|2018.03}}
First 8 of an infinite sequence
<syntaxhighlight lang="raku" perl6line>.say for (0, { '0' ~ @_.join.trans( "01" => "10", :g) } ... *)[^8];</langsyntaxhighlight>
 
{{out}}
Line 1,969 ⟶ 2,509:
01101001100101101001011001101001100101100110100101101001100101101001011001101001011010011001011001101001100101101001011001101001
^C</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <ThueMorse 7>>
};
 
ThueMorse {
0 e.X = e.X;
s.N e.X = <ThueMorse <- s.N 1> <ThueMorseStep e.X>>;
};
 
ThueMorseStep {
= '0';
e.X = e.X <Invert e.X>;
};
 
Invert {
= ;
'0' e.X = '1' <Invert e.X>;
'1' e.X = '0' <Invert e.X>;
};</syntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
 
=={{header|REXX}}==
===using functions===
Programming note: &nbsp; ''pop count'' &nbsp; (or &nbsp; ''weight'') &nbsp; is the number of &nbsp; <b>1</b>'s &nbsp; bits in the binary representation of a number.
<langsyntaxhighlight lang="rexx">/*REXX pgm generates & displays the Thue─Morse sequence up to the Nth term (inclusive). */
parse arg N . /*obtain the optional argument from CL.*/
if N=='' | N=="," then N= 80 /*Not specified? Then use the default.*/
Line 1,984 ⟶ 2,547:
/*──────────────────────────────────────────────────────────────────────────────────────*/
$pop: return length( space( translate( arg(1), , 0), 0) ) /*count 1's in number.*/
$weight: return $pop( x2b( d2x( arg(1) ) ) ) /*dec──►bin, pop count*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,991 ⟶ 2,554:
 
===using in-line code===
<langsyntaxhighlight lang="rexx">/*REXX pgm generates & displays the Thue─Morse sequence up to the Nth term (inclusive). */
parse arg N . /*obtain the optional argument from CL.*/
if N=='' | N=="," then N= 80 /*Not specified? Then use the default.*/
Line 1,998 ⟶ 2,561:
$= $ || length( space( translate( x2b( d2x(j) ), , 0), 0)) // 2 /*append to $.*/
end /*j*/
say $ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br>
 
Line 2,005 ⟶ 2,568:
 
Because of this, the displaying of the output lacks the granularity of the first two REXX versions.
<langsyntaxhighlight lang="rexx">/*REXX pgm generates & displays the Thue─Morse sequence up to the Nth term (inclusive). */
parse arg N . /*obtain the optional argument from CL.*/
if N=='' | N=="," then N= 6 /*Not specified? Then use the default.*/
Line 2,012 ⟶ 2,575:
$= $ || translate($, 10, 01) /*append $'s complement to $ string.*/
end /*j*/
say $ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,019 ⟶ 2,582:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
tm = "0"
see tm
Line 2,034 ⟶ 2,597:
see nl
return (previous + tm)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,044 ⟶ 2,607:
01101001100101101001011001101001
0110100110010110100101100110100110010110011010010110100110010110
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
We use here the bitwise negation method: it needs few words and is actually faster than the "fast" generation method, because RPL is better in list handling than in bitwise calculations.
≪ { 0 }
'''WHILE''' DUP2 SIZE > '''REPEAT'''
1 OVER - +
'''END'''
1 ROT SUB
≫ '<span style="color:blue">THUEM</span>' STO
 
20 <span style="color:blue">THUEM</span>
{{out}}
<pre>
1: { 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">puts s = "0"
6.times{puts s << s.tr("01","10")}</langsyntaxhighlight>
 
{{out}}
Line 2,062 ⟶ 2,641:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">const ITERATIONS: usize = 8;
 
fn neg(sequence: &String) -> String {
Line 2,078 ⟶ 2,657:
sequence = format!("{}{}", sequence, neg(&sequence));
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,093 ⟶ 2,672:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def thueMorse(n: Int): String = {
val (sb0, sb1) = (new StringBuilder("0"), new StringBuilder("1"))
(0 until n).foreach { _ =>
Line 2,103 ⟶ 2,682:
}
 
(0 to 6).foreach(i => println(s"$i : ${thueMorse(i)}"))</langsyntaxhighlight>
{{Out}} See it running in your browser by [https://scastie.scala-lang.org/rsF3Y5ABQoK0zZMMA3m6Ow Scastie (JVM)].
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func recmap(repeat, seed, transform, callback) {
func (repeat, seed) {
callback(seed)
Line 2,114 ⟶ 2,693:
}
 
recmap(6, "0", {|s| s + s.tr('01', '10') }, { .say })</langsyntaxhighlight>
{{out}}
<pre>
Line 2,128 ⟶ 2,707:
=={{header|SQL}}==
This example is using SQLite.
<langsyntaxhighlight SQLlang="sql">with recursive a(a) as (select '0' union all select replace(replace(hex(a),'30','01'),'31','10') from a) select * from a;</langsyntaxhighlight>
You can add a LIMIT clause to the end to limit how many lines of output you want.
 
Line 2,135 ⟶ 2,714:
Since <tt>string map</tt> correctly handles overlapping replacements, the simple map <tt>0 -> 01; 1 -> 10</tt> can be applied with no special handling:
 
<langsyntaxhighlight Tcllang="tcl">proc tm_expand {s} {string map {0 01 1 10} $s}
# this could also be written as:
# interp alias {} tm_expand {} string map {0 01 1 10}
Line 2,145 ⟶ 2,724:
}
return $s
}</langsyntaxhighlight>
 
Testing:
 
<langsyntaxhighlight Tcllang="tcl">for {set i 0} {$i <= 6} {incr i} {
puts [tm $i]
}</langsyntaxhighlight>
 
{{out}}
Line 2,164 ⟶ 2,743:
For giggles, also note that the above SQL solution can be "natively" applied in Tcl8.5+, which bundles Sqlite as a core extension:
 
<syntaxhighlight lang="tcl">
<lang Tcl>
package require sqlite3 ;# available with Tcl8.5+ core
sqlite3 db "" ;# create in-memory database
Line 2,170 ⟶ 2,749:
db eval {with recursive a(a) as (select '0' union all select replace(replace(hex(a),'30','01'),'31','10') from a) select a from a limit $LIMIT} {
puts $a
}</langsyntaxhighlight>
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">For x = 0 to 6 ' sequence loop
Print Using "_#";x;": "; ' print sequence
For y = 0 To (2^x)-1 ' element loop
Line 2,190 ⟶ 2,769:
a@ = SHL(a@, -1) ' shift the number
Loop
Return (b@) ' return number of bits set</langsyntaxhighlight>
{{Out}}
<pre> 0: 0
Line 2,203 ⟶ 2,782:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Option Explicit
 
Sub Main()
Line 2,224 ⟶ 2,803:
End If
Thue_Morse = s & k
End Function</langsyntaxhighlight>
{{Out}}
<pre>1:=> 0
Line 2,237 ⟶ 2,816:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 2,256 ⟶ 2,835:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>0110100110010110100101100110100110010110011010010110100110010110</pre>
Line 2,262 ⟶ 2,841:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var thueMorse = Fn.new { |n|
var sb0 = "0"
var sb1 = "1"
Line 2,273 ⟶ 2,852:
}
 
for (i in 0..6) System.print("%(i) : %(thueMorse.call(i))")</langsyntaxhighlight>
 
{{out}}
Line 2,287 ⟶ 2,866:
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="lisp">(defun thue-morse (n)
(defun flip-bits (s)
(defun flip (l)
Line 2,309 ⟶ 2,888:
; test THUE-MORSE by printing the strings it returns for n = 0 to n = 6
 
(mapcar (lambda (n) (print (thue-morse n))) (range 0 7))</langsyntaxhighlight>
{{out}}
<pre>"0"
Line 2,320 ⟶ 2,899:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated strings
char Thue;
int N, I, J;
Line 2,336 ⟶ 2,915:
J:= J+I; \set index to terminator
];
]</langsyntaxhighlight>
 
{{out}}
Line 2,351 ⟶ 2,930:
=={{header|Yabasic}}==
{{trans|Phix}}
<langsyntaxhighlight Yabasiclang="yabasic">tm$ = "0"
for i=1 to 8
? tm$
Line 2,364 ⟶ 2,943:
next
return tm$
end sub</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn nextTM(str){ str.pump(str,'-.fp("10")) } // == fcn(c){ "10" - c }) }</langsyntaxhighlight>
"12233334444" - "23"-->"14444"
<langsyntaxhighlight lang="zkl">str:="0"; do(7){ str=nextTM(str.println()) }</langsyntaxhighlight>
println() returns the result it prints (as a string).
{{trans|Java}}
<langsyntaxhighlight lang="zkl">fcn nextTM2{
var sb1=Data(Void,"0"), sb2=Data(Void,"1");
r:=sb1.text; sb1.append(sb2); sb2.append(r);
r
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">do(7){ nextTM2().println() }</langsyntaxhighlight>
{{out}}
<pre>
2,171

edits