Hi, list! I have a problem with some graphics in Metapost. I've tried to replicate the Koch curve cited here using ConTeXt:

https://en.wikipedia.org/wiki/L-system

To my own amusement, the patterns seem to appear. However, when I try with a number from 6, ConTeXt stops doing anything or output looks wrong (see attached PDF, please).

\startluacode
userdata = userdata or {}
 
function userdata.koch(n)
 
local init = "F"
local base = init
  local replacement = "F+F-F-F+F"
 
for i = 1, n do
init = init:gsub(base, replacement)
end

return init
 
end

function userdata.kochlen(n)
return string.len(userdata.koch(n))
end

function userdata.kochchar(k, n)
return string.sub(userdata.koch(n),k,k)
end

function MP.kochlen(n)
mp.print(userdata.kochlen(n))
end

function MP.kochchar(k,n)
mp.quoted(userdata.kochchar(k,n))
end

\stopluacode
\startMPpage[instance=doublefun]

def koch_curve(expr n, u) =

save p; save q;
save angle;

pair p, last; path q; numeric angle;
p:= origin;
q := origin;
angle := 0;

for i = 1 upto lua.MP.kochlen(n):
if lua.MP.kochchar(i,n) = "F":
q := q -- ((point infinity of q) + dir(angle));
else:
if lua.MP.kochchar(i,n) = "+":
angle := (angle + 90) mod 360;
else:
if lua.MP.kochchar(i,n) = "-":
angle := (angle - 90) mod 360;
fi
fi
fi
endfor;
draw q scaled u;
enddef;

koch_curve(5, 5mm);
%koch_curve(6, 5mm); does not give a right output

\stopMPpage

Maybe there's a fabulous trick to make it work with larger numbers, but I don't know it. I'm aware of a TikZ library for Lindenmayer systems, but it's not a possibility for me in this case (I add the remark just in case).

Thank you in advance,

Jairo :)