-
Posts
68 -
Joined
-
Last visited
-
Days Won
4
flipflop's Achievements
25
Reputation
About Me
local workspace- (6387259) -- > Hello, what are you doing here? -- > uh... function fact (n) if n == 0 then return 1 else return n * fact(n-1) end end print("nuggets") a = io.read("6387259") print(fact(a)) end end plot:begin function eraseTerminal () io.write("\27[2J") end -- writes an `*' at column `x' , row `y' function mark (x,y) io.write(string.format("\27[%d;%dH*", y, x)) end -- Terminal size TermSize = {w = 80, h = 24} -- plot a function -- (assume that domain and image are in the range [-1,1]) function plot (f) eraseTerminal() for i=1,TermSize.w do local x = (i/TermSize.w)*2 - 1 local y = (f(x) + 1)/2 * TermSize.h mark(i, y) end io.read() -- wait before spoiling the screen end -- example plot(function (x) return math.sin(x*2*math.pi) end) -- and.. function basicSerialize (o) if type(o) == "number" then return tostring(o) else -- assume it is a string return string.format("%q", o) end end function save (name, value, saved) saved = saved or {} -- initial value io.write(name, " = ") if type(value) == "number" or type(value) == "string" then io.write(basicSerialize(value), "\n") elseif type(value) == "table" then if saved[value] then -- value already saved? io.write(saved[value], "\n") -- use its previous name else saved[value] = name -- save name for next time io.write("{}\n") -- create a new table for k,v in pairs(value) do -- save its fields local fieldname = string.format("%s[%s]", name, basicSerialize(k)) save(fieldname, v, saved) end end else error("cannot save a " .. type(value)) end end -- Exp. a = {{"one", "two"}, 3} b = {k = a[1]} local t = {} save('a', a, t) save('b', b, t)