1 2 3 4 5 6 7 8 9 | -- example of an anonymous function -- returned as a value -- see http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/hopl.pdf function add(x) return function (y) return (x + y) end end f = add(2) print(type(f), f(10)) function 12 |
1 2 3 4 | i = 3 a = {1, 3, 5, 7, 9} i, a, a[i+1], b = i+1, a[i+1], a print (i, a[3], a[4], b, I) |
1 | # apt-get install lua50 |
1 2 3 4 5 6 7 8 9 10 11 12 | -- defines a factorial function function fact (n) if n == 0 then return 1 else return n * fact(n-1) end end print("enter a number:") a = io.read("*number") print(fact(a)) |
1 2 3 4 | $ lua factorial.lua enter a number: 10 3628800 |
1 2 3 4 5 6 | $ (echo '#! /usr/bin/lua'; cat factorial.lua) > factorial $ chmod u+x factorial $ ./factorial enter a number: 4 24 |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |