[M3devel] looping over range of integers in quake

Jay K jay.krell at cornell.edu
Fri Jul 2 13:04:54 CEST 2010


Given the lack of lexical scoping and closures, the second form is preferred.
  Actually there is lexical scoping it seems, but not closures. Maybe more on that later.

proc For(start, endd, body) is
  if equal(start, endd & "")
    return
  end
  body(start)
  For(Add(start, 1), endd, body)
end


For(0, 10, write)
write(CR)


proc ToString(a) is
  return a & ""
end


proc Range(start, endd) is
% I thought local a good idea here but no.
  proc Helper(result, start, endd) is
    if equal(start, endd)
      return result
    end
    result += start
    return Helper(result, Add(start, 1), endd)
  end
  return Helper([ ], ToString(start), ToString(endd))
end


foreach i in Range(0, 10)
  write(i, CR)
end



 		 	   		  


More information about the M3devel mailing list