lua学习

这几天在看一个叫murphy的东西,主要是管理系统的音频。用到了lua,特此记录一下

--run this programe  dofile("lib1.lua")

function norm (x, y)
	local n2 = x^2 + y^2
	return math.sqrt(n2)
end
		 
function twice (x)
	return 2*x
end

--  此处x为全局变量
x=10
local i =1

while i<=x do
	local x = i*2 -- 此处x为局部变量
	print(x)
	i=i+1
end

--[[
--多行注释
--尽可能的使用局部变量,因为访问局部变量要比访问全局变量快
--]]
--
if i>20 then 
	local x
	x=20
	print(x+2)
else
	print(x)
end

print(x)

--  if语句的三种形式
if i>10 then
	print()
end

if i==10 then
	print()
else
	print()
end

if i==10 then
	print()
elseif i>10 then
	print()
elseif i>11 then
	print()
else
	print()
end

-- while 循环
while i<10 do
	print()
end

-- for循环
for var=1,10 do 
	print(var)
end

local found = nil
for i=1,10 do
	if i == 5 then
		found = i
		break;
	end
end

print("found="..found)


local a1 = "hello"
local a2 = "world"
print(a1..a2)  --  ..相当于C++中的+运算符  

-- 返回多个值
s, e = string.find("hello Lua users", "Lua")
print(s,e)

function mainnum(a)
	local mi =1;
	local m = a[mi]
	for i,val in ipairs(a) do
		if val >m then
			mi = i
			m = val
		end
	end
	return m ,mi
end

print(mainnum({8,24,36,2,14,78,25}))

-- lua的闭包原则
function newCounter()
	local i = 0
	return function()
		i = i+1
		return i
	end
end

c1 = newCounter()
print(c1()) --> 1
print(c1()) --> 2

c2 = newCounter()
print(c2()) --> 1
print(c1()) --> 3 
print(c2()) --> 2

-- ***************非全局函数
-- 表和函数放在一起
Lib = {}
Lib.foo = function(x,y) return x+y end
Lib.goo = function(x,y) return x-y end

-- 使用表构造函数 
Lib = {

	foo = function(x,y) return x+y end,
	goo = function(x,y) return x-y end
}

-- 使用局部函数
local f =function(...)
-- ...
end

local g =function(...)
	f()
end

local function f(...)
end

-- 声明递归的局部函数
local fact = function(n)
	if n ==0 then
		return 1
	else
		return n*fact(n-1) -- 此处查找辉查找fact是否为全局函数
	end
end

--  正确的声明递归的局部函数如下
local fact 
fact = function(n)
	if n ==0 then
		return 1
	else
		return n*fact(n-1) -- 此处查找辉查找fact是否为全局函数
	end
end

--正确的用
function f(x)
	return g(x)
end
-- n(x)可以是一个复杂的表达式,此种调用也算是尾调用
function m(x)
	return n(x[j],i+j)
end

-- 错误的尾调用
function f(x)
	return g(x) +1 
end


function room1 ()
    local move = io.read()
    if move == "south" then
       return room3()
    elseif move == "east" then
       return room2()
    else
       print("invalid move")
       return room1()   -- stay in the same room
    end
end
 
function room2 ()
    local move = io.read()
    if move == "south" then
       return room4()
    elseif move == "west" then
       return room1()
    else
       print("invalid move")
       return room2()
    end
end
 
function room3 ()
    local move = io.read()
    if move == "north" then
       return room1()
    elseif move == "east" then
       return room4()
    else
       print("invalid move")
       return room3()
    end
end
 
function room4 ()
    print("congratilations!")
end


-- 迭代器与闭包
function list_iter(t)
	local i =0
	local n = table.getn(t)
	return function()
		i = i+1
		if i<=n then return t[i] end
	end
end

t = {10,20,30,40}
iter  = list_iter(t)
while true do
	local element = iter()
	if element == nil then break end
	print(element)
end

for element in list_iter(t) do
	print(element)
end

-- loadstring 生成一个函数调用
f = loadstring("local a = 10; return a+20") 
print(f())  --> 30

lua的语法比较怪,不太适应,可以直接使用全部变量而不用申明。可以返回多个值,以上的只是学习的一部分,以后会慢慢补充

原文地址:https://www.cnblogs.com/jjxxjnzy/p/3110701.html