

Task
Produce a graphical or ASCII-art representation of a Hilbert curve of at least order 3.

hilbert(6) : turtle(_);
 
fcn hilbert(n){	// Lindenmayer system --> Data of As & Bs
   var [const] A="-BF+AFA+FB-", B="+AF-BFB-FA+";
   buf1,buf2 := Data(Void,"A").howza(3), Data().howza(3);  // characters
   do(n){
      buf1.pump(buf2.clear(),fcn(c){ if(c=="A") A else if(c=="B") B else c });
      t:=buf1; buf1=buf2; buf2=t;	// swap buffers
   }
   buf1		// n=6 --> 13,651 letters
}
 
fcn turtle(hilbert){
   const D=10;
   ds,dir := T( T(D,0), T(0,-D), T(-D,0), T(0,D) ), 0;  // turtle offsets
   dx,dy := ds[dir];
   img:=PPM(650,650); x,y:=10,10; color:=0x00ff00;
   hilbert.replace("A","").replace("B","");  // A & B are no-op during drawing
   foreach c in (hilbert){
      switch(c){
	 case("F"){ img.line(x,y, (x+=dx),(y+=dy), color) }  // draw forward
	 case("+"){ dir=(dir+1)%4; dx,dy = ds[dir] } // turn right 90*
	 case("-"){ dir=(dir-1)%4; dx,dy = ds[dir] } // turn left 90*
      }
   }
   img.writeJPGFile("hilbert.zkl.jpg");
}