首页 > 其他 > 详细

Programming in Lua习题选答

时间:2016-06-28 21:50:32      阅读:148      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!