! plots nowhere differentiable curve

def frac(x)=x-int(x)
def h(x) = .5*(1-abs(2*frac(x)-1))
def f(x,n)=(1/(2^n))* h((4^n)*x)
def g(x,n)
  let total=0
  for k=0 to n
    let total=total+f(x,k)
  next k
  let g=total
end def

 let a= 0
 let b= 1
 let xmin=0
 let xmax=1
 let ymin=0
 let ymax=1

let frames=10
dim pic$(0 to 10)

set mode "egahires"

open #1 : screen 0.25,.75,.2,1

window #1
set window xmin,xmax,ymin,ymax     
clear

sub plotf
  let m=640
  let dx = (b-a)/m
  let x=a
  let y = f(x,n)
  plot x,y ;
  for i = 1 to m
    let x=x+dx
    let y = f(x,n)
    plot x,y;
  next i
  plot
end sub

sub plotg
  let m=640
  let dx = (b-a)/m
  let x=a
  let y = g(x,n)
  plot x,y ;
  for i = 1 to m
    let x=x+dx
    let y = g(x,n)
    plot x,y;
  next i
  plot
end sub

sub savepic(n)
! save as many frames as possible until Out of Memory
  when error in
   box keep xmin,xmax,ymin,ymax in pic$(n)
  use
   if maxframes=savedframes then let savedframes=n
  end when
end sub

let n=0
set color "white"
call plotf
let n=1
set color "yellow"
call plotf
let n=2
set color "red"
call plotf
set color "blue"
call plotg
line input a$
set color "white"

let maxframes  =frames
let savedframes=frames
for n=0 to maxframes
  clear
  plot text,at .2,.8: str$(n)
  call plotg
  call savepic(n)
next n

let n=0

do
  box show pic$(n) at xmin,ymin
  let n=n+1
  if n>savedframes then let n=0
  for i= 1 to 100000
  next i
loop
end
