码迷,mamicode.com
首页 > 其他好文 > 详细

Programming in Lua习题选答

时间:2016-06-28 21:50:47      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

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习题选答

标签:

原文地址:http://www.cnblogs.com/90programmer/p/5624973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!