lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Philippe Lhoste wrote:
Some languages like C or Java have dozens of entries, so I thought: why not users of the Lua communauty doesn't contribute more quines?

I suggest you send them here, I will collect them and send them at once (but of course you can send them separately if you want).

Wow, I did it! I wasn't sure I could make one...
Actually, I stole two ideas from other quines: to use variables to store sensitive characters, and to use an array to store the program.

I made two variants: a compact one, and a more nicely indented, with explicit variable names one.

C:\Dev\LuaPhiLhoSoft> Lua-5.0.exe Quine5-2.lua > q5-2.lua
C:\Dev\LuaPhiLhoSoft> diff Quine5-2.lua q5-2.lua
C:\Dev\LuaPhiLhoSoft> Lua-5.0.exe Quine5-3.lua > q5-3.lua
C:\Dev\LuaPhiLhoSoft> diff Quine5-3.lua q5-3.lua

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --
-- Lua 5.01 Quine 2 by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr
local q, r = string.char(39), string.char(10)
function D(a) for i, v in ipairs(a) do io.write(v .. r) end end
function B(a) local s = "" for i, v in ipairs(a) do s = s .. q .. v .. q .. "," .. r end return s end
a = {
'-- Lua 5.01 Quine 2 by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr',
'local q, r = string.char(39), string.char(10)',
'function D(a) for i, v in ipairs(a) do io.write(v .. r) end end',
'function B(a) local s = "" for i, v in ipairs(a) do s = s .. q .. v .. q .. "," .. r end return s end',
'',
'a[5] = "a = {" .. r .. B(a) .. "}"',
'D(a)',
}
a[5] = "a = {" .. r .. B(a) .. "}"
D(a)
-- Lua 5.01 Quine 3
-- by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr

local openString, closeString =
	string.char(91, 91, 10), string.char(93, 93, 44, 10)

function DumpArray(array)
	for i, val in ipairs(array) do
		print(val)
	end
end

function BuildArray(array)
	local s = ""
	for i, val in ipairs(array) do
		s = s .. openString .. val .. closeString
	end
	return s
end

programArray =
{
[[
-- Lua 5.01 Quine 3
-- by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr
]],
[[
local openString, closeString =
	string.char(91, 91, 10), string.char(93, 93, 44, 10)
]],
[[
function DumpArray(array)
	for i, val in ipairs(array) do
		print(val)
	end
end
]],
[[
function BuildArray(array)
	local s = ""
	for i, val in ipairs(array) do
		s = s .. openString .. val .. closeString
	end
	return s
end
]],
[[
]],
[[
programArray[5] = [[
programArray =
{
]] ..
	BuildArray(programArray) ..
[[
}
]]
]],
[[
DumpArray(programArray)
]],
}

programArray[5] = [[
programArray =
{
]] ..
	BuildArray(programArray) ..
[[
}
]]

DumpArray(programArray)