Re: Code Puzzles
Posted: Thu Dec 10, 2015 10:24 pm
I'm interested in seeing how it's done without redefining print.airstruck wrote:It can be done without messing with print, but the solutions won't be so minimal.
I'm interested in seeing how it's done without redefining print.airstruck wrote:It can be done without messing with print, but the solutions won't be so minimal.
bartbes wrote:Or if you're opposed to debug.setmetatable, set a metatable on _G that wraps functions when they are set, left as an exercise for the reader.
Here is one:davisdude wrote:I'm interested in seeing how it's done without redefining print.
Code: Select all
function T(f)
f(function(g) g(T) f(function()end) end)
end
Code: Select all
s{
function(w)
w(6)
print("not")
end,
function(w)
for i = 1,4 do
print("fuzzy")
w(2)
end
end,
function(w)
for i = 1,3 do
print("wuzzy")
w(2)
end
end,
function(w)
w(1)
print("was")
w(2)
for i = 1,2 do
w(2)
print("was")
end
end,
function(w)
w(1)
print("a bear")
w(2)
print("had no hair")
w(4)
print("he")
end
}
fuzzy
wuzzy
was
a bear
fuzzy
wuzzy
had no hair
fuzzy
wuzzy
was
not
fuzzy
was
he
Although this doesn't work if it's nested any more than two levels.vrld wrote:Here is one:davisdude wrote:I'm interested in seeing how it's done without redefining print.Code: Select all
function T(f) f(function(g) g(T) f(function()end) end) end
It solves the puzzle ...davisdude wrote:Although this doesn't work if it's nested any more than two levels.
(When I say "work," I mean that it doesn't repeat the printed statement above then below, which is how I expect it to work, at least).
Code: Select all
function T(f)
local function t(g)
T(g)
f(function() end)
end
f(t)
end