lua-users home
lua-l archive

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


For newbies like me...

-- gcd.lua
-- Greatest common divisor

-- Unecessary, here just to test argument passing
-- use lua -f gcd.lua 1233 12, for example
if arg then
	print("Arguments:")
	for i, v in arg do
		print(i, v)
	end
	print""
end

-- Take numerical arguments if exists,
-- then check if there is a value, otherwise take default value
local v1 = tonumber((arg and arg[1])) or (v1 or 121)
local v2 = tonumber((arg and arg[2])) or (v2 or 1221)

function gcd_plain(m, n)
	-- Using classical temporary variable
	local t
	while m ~= 0 do
		t = m
		m = mod(n, m)
		n = t
	end
	return n
end

function gcd_hip(m, n)
	-- Using parallel assignment
	while m ~= 0 do
		m, n = mod(n, m), m
	end
	return n
end

write(format("Greatest common divisor of %d and %d is %d\n", v1, v2,
gcd_plain(v1, v2)))
write(format("Greatest common divisor of %d and %d is %d\n", v1, v2,
gcd_hip(v1, v2)))

Made this because I read the article at
http://www.byte.com/column/BYT20010410S0001
that stress out the capability of Python to do parallel assignments.
See also http://www.python.org/doc/essays/ppt/acm-ws/sld045.htm
Lua can do it too! :-)

Lua team can put this in the examples, if they find it useful enough.

Regards.

--._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`--

> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Markus Huber
> Sent: Tuesday, April 17, 2001 3:08 PM
> To: Multiple recipients of list
> Subject: Chars for Identifiers
>
>
> > "Luiz Henrique de Figueiredo" wrote:
>
> > > "Markus Huber" wrote:
>
> > > > "Gavin Wraith" wrote:
>
> > > I have tested and found that also possible are the characters ?!$@
>
> > Really? You tested it?
>
> Yes.
>
> > What version and what platform?
>
> RISC OS
>
> > If this is right, it seems to me that it's a bug in locale...
>
> > > > The possibility of using @!?$ is a feature of RiscLua, not Lua
> > > > proper.
>
> > > Whoops! The discussion group doesn't understand my question :-)
>
> > > > RiscLua deviates from Lua both in this particular
>
> > > I should read the difference manual again.
>
> > > > and also in allowing hexadecimal literal numbers.
>
> > > I have read this...
>
> > > >  As I think I stated in the documentation, you must avoid
> > > > these features if you want your Lua script to be portable.
>
> > > Yes. I will avoid that. Sometimes with @!?$ sometimes without is a
> > > little bit confusing.
>
> > > > As Lua does not at present have any special syntactic use
> > > > for these extra symbols
>
> > > I think it would be better to use a special char for Luas special
> > > variables but only if comptaible with all versions. The authors
> > > doesn't think not so and now its done. _<uppercase> seems not very Lua
> > > clever. Instead @<A-Z a-z 0-9 _> for special vars to be stringent
> > > with the identifier definition. Then the next possible step is that
> > > all identifiers with @... are case insensitive. So I can use @Error
> > > and @error without any difference and everyone can write special
> > > variables/identifiers in his/her prefered style.
>
> > > > I decided it would do no harm to modify the parser to allow them,
> > > > but of course it is NOT Lua, and users are advised to stick with
> > > > the Lua constraints on identifiers.
>
> > > Thank you for your explanation.
>
> > Accented chars are allowed if the locale says they are letters. The
> > lexer simply uses isalnum.
>
> > Anyway. If you want your Lua programs to be portable, then you cannot
> > use those characters, and have to stick to _ 0-9 A-Z a-z.
>
> I will do that. Thank you for your help.
>
>
> Markus
>