Writing Lua on Mac

Getting started with Lua on Mac: install via Homebrew and learn basic Lua syntax with examples including Hello World and a recursive factorial function.

Install lua on mac

I’m not sure that whether Lua is built on mac originally.

(Ok, tested on Mac OSX 10.9, there is Lua in it.)

So I installed Lua via Homebrew.

Install homebrew (optional)

1
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Install Lua by homebrew

1
brew install lua

Writing Lua

You can use command: lua to interact with lua. (just like php -a or irb)

1
print("Hello World")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function fact(n)
	if n == 0 then
		return 1
	else
		return n * fact(n-1)
	end
end

print("enter a number:")
num = io.read("*number") 
print(fact(num))
		
comments powered by Disqus
Powered by Hugo. Theme Stack. All Rights Reserved.