Programming in Lua习题选答

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Programming in Lua习题选答相关的知识,希望对你有一定的参考价值。

Exercise 5.1

1 function concatenate (...)
2     local s = ""
3     for i, v in ipairs{...} do
4         s = s .. v
5     end
6     return s
7 end

 Exercise 5.2

 1 -- print all elements in array
 2 function print_array (array) 
 3     for key, value in pairs(array) do
 4         print("key: " .. key .. " --> value: " .. value)
 5     end
 6 end
 7 
 8 arr = {name = "alice", age = 18}
 9 print_array(arr)
10 
11 -- if the function has one single argument and that argument is
12 -- either a literal string or a table constructor, then the parentheses
13 -- is optional.
14 print_array{name = "tom", age = 20}

 Exercise 5.3

1 -- receive an arbitrary number of values and 
2 -- return all of them, except the frist one
3 function remove_first(first, ...)
4     return ...
5 end
6 
7 // test
8 print(remove_first("abc", "def", "hij"))

 

以上是关于Programming in Lua习题选答的主要内容,如果未能解决你的问题,请参考以下文章

Programming In Lua 第八章

Programming In Lua 第三章

Programming In Lua 第四章

Programming In Lua 第五章

Programming In Lua 第十章

笔记 | Programming in Lua