Fun GEL code

Cut and paste this code to a new program and press "Run."

Cantor function (devil's staircase)

Here is code to plot the Cantor function. It does 20 steps which is more than sufficient. Make sure to plot "Cantor" only for x between 0 and 1. Otherwise it's set to constant 0 and 1.

function Ternary(x,n) = (

# only if x in [0,1)

out = null;

for k = 1 to n do (

x = x*3;

fx = floor (x);

x = FractionalPart (x);

out = [out, fx]

)

)

# Cantor function, only really for x in [0,1]

function Cantor(x) = (

if x >= 1 then return 1;

if x <= 0 then return 0;

# Number of steps, the higher the number the more precise the graph

n = 20;

t = Ternary(x,n);

N = n;

for k = 1 to n do (

if t@(k) == 1 then

(N = k; break)

);

1/(2^N) + sum k = 1 to (N-1) do (t@(k)/(2^(k+1)))

)