Difference between revisions of "Lua"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Comments)
Line 12: Line 12:
 
=Variables=
 
=Variables=
 
===Numbers===
 
===Numbers===
 +
<syntaxhighlight lang=lua>
 
num = 42  -- All numbers are doubles.
 
num = 42  -- All numbers are doubles.
 +
</syntaxhighlight>
  
 
===Strings===
 
===Strings===
 +
<syntaxhighlight lang=lua>
 
s = 'walternate'  -- Immutable strings like Python.
 
s = 'walternate'  -- Immutable strings like Python.
 
t = "double-quotes are also fine"
 
t = "double-quotes are also fine"
Line 20: Line 23:
 
       start and end
 
       start and end
 
       multi-line strings.]]
 
       multi-line strings.]]
 +
</syntaxhighlight>
  
 
===Empty / Null===
 
===Empty / Null===
 +
<syntaxhighlight lang=lua>
 
t = nil  -- Undefines t; Lua has garbage collection.
 
t = nil  -- Undefines t; Lua has garbage collection.
 +
</syntaxhighlight>
  
 
+
=If Statement=
 
+
<syntaxhighlight lang=lua>
-- If clauses:
 
 
if num > 40 then
 
if num > 40 then
 
   print('over 40')
 
   print('over 40')
elseif s ~= 'walternate' then  -- ~= is not equals.
+
elseif s ~= 'walternate' then   
 +
  -- ~= is not equals.
 
   -- Equality check is == like Python; ok for strs.
 
   -- Equality check is == like Python; ok for strs.
 
   io.write('not over 40\n')  -- Defaults to stdout.
 
   io.write('not over 40\n')  -- Defaults to stdout.
Line 42: Line 48:
 
   print('Winter is coming, ' .. line)
 
   print('Winter is coming, ' .. line)
 
end
 
end
 +
</syntaxhighlight>
  
 
-- Undefined variables return nil.
 
-- Undefined variables return nil.
Line 58: Line 65:
 
=Loops=
 
=Loops=
 
==While==
 
==While==
 +
<syntaxhighlight lang=lua>
 
-- Blocks are denoted with keywords like do/end:
 
-- Blocks are denoted with keywords like do/end:
 
while num < 50 do
 
while num < 50 do
 
   num = num + 1  -- No ++ or += type operators.
 
   num = num + 1  -- No ++ or += type operators.
 
end
 
end
 +
</syntaxhighlight>
  
 
==For==
 
==For==
 +
<syntaxhighlight lang=lua>
 
karlSum = 0
 
karlSum = 0
 
for i = 1, 100 do  -- The range includes both ends.
 
for i = 1, 100 do  -- The range includes both ends.
Line 74: Line 84:
  
 
-- In general, the range is begin, end[, step].
 
-- In general, the range is begin, end[, step].
 +
</syntaxhighlight>
  
 
==Repeat / Do While==
 
==Repeat / Do While==
 +
<syntaxhighlight lang=lua>
 
-- Another loop construct:
 
-- Another loop construct:
 
repeat
 
repeat
Line 81: Line 93:
 
   num = num - 1
 
   num = num - 1
 
until num == 0
 
until num == 0
 
+
</syntaxhighlight>
 
 
=Functions=
 
 
 
function fib(n)
 
  if n < 2 then return 1 end
 
  return fib(n - 2) + fib(n - 1)
 
end
 
 
 
-- Closures and anonymous functions are ok:
 
function adder(x)
 
  -- The returned function is created when adder is
 
  -- called, and remembers the value of x:
 
  return function (y) return x + y end
 
end
 
a1 = adder(9)
 
a2 = adder(36)
 
print(a1(16))  --> 25
 
print(a2(64))  --> 100
 
 
 
-- Returns, func calls, and assignments all work
 
-- with lists that may be mismatched in length.
 
-- Unmatched receivers are nil;
 
-- unmatched senders are discarded.
 
 
 
x, y, z = 1, 2, 3, 4
 
-- Now x = 1, y = 2, z = 3, and 4 is thrown away.
 
 
 
function bar(a, b, c)
 
  print(a, b, c)
 
  return 4, 8, 15, 16, 23, 42
 
end
 
 
 
x, y = bar('zaphod')  --> prints "zaphod  nil nil"
 
-- Now x = 4, y = 8, values 15..42 are discarded.
 
 
 
-- Functions are first-class, may be local/global.
 
-- These are the same:
 
function f(x) return x * x end
 
f = function (x) return x * x end
 
 
 
-- And so are these:
 
local function g(x) return math.sin(x) end
 
local g; g  = function (x) return math.sin(x) end
 
-- the 'local g' decl makes g-self-references ok.
 
 
 
-- Trig funcs work in radians, by the way.
 
 
 
-- Calls with one string param don't need parens:
 
print 'hello'  -- Works fine.
 

Revision as of 15:19, 2 June 2019

Comments

-- Two dashes start a one-line comment.

--[[
     Adding two ['s and ]'s makes it a
     multi-line comment.
--]]

Variables

Numbers

num = 42  -- All numbers are doubles.

Strings

s = 'walternate'  -- Immutable strings like Python.
t = "double-quotes are also fine"
u = [[ Double brackets
       start and end
       multi-line strings.]]

Empty / Null

t = nil  -- Undefines t; Lua has garbage collection.

If Statement

if num > 40 then
  print('over 40')
elseif s ~= 'walternate' then  
  -- ~= is not equals.
  -- Equality check is == like Python; ok for strs.
  io.write('not over 40\n')  -- Defaults to stdout.
else
  -- Variables are global by default.
  thisIsGlobal = 5  -- Camel case is common.

  -- How to make a variable local:
  local line = io.read()  -- Reads next stdin line.

  -- String concatenation uses the .. operator:
  print('Winter is coming, ' .. line)
end

-- Undefined variables return nil. -- This is not an error: foo = anUnknownVariable -- Now foo = nil.

aBoolValue = false

-- Only nil and false are falsy; 0 and are true! if not aBoolValue then print('twas false') end

-- 'or' and 'and' are short-circuited. -- This is similar to the a?b:c operator in C/js: ans = aBoolValue and 'yes' or 'no' --> 'no'

Loops

While

-- Blocks are denoted with keywords like do/end:
while num < 50 do
  num = num + 1  -- No ++ or += type operators.
end

For

karlSum = 0
for i = 1, 100 do  -- The range includes both ends.
  karlSum = karlSum + i
end

-- Use "100, 1, -1" as the range to count down:
fredSum = 0
for j = 100, 1, -1 do fredSum = fredSum + j end

-- In general, the range is begin, end[, step].

Repeat / Do While

-- Another loop construct:
repeat
  print('the way of the future')
  num = num - 1
until num == 0