2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
29
Task/Catamorphism/Lua/catamorphism.lua
Normal file
29
Task/Catamorphism/Lua/catamorphism.lua
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
table.unpack = table.unpack or unpack -- 5.1 compatibility
|
||||
local nums = {1,2,3,4,5,6,7,8,9}
|
||||
|
||||
function add(a,b)
|
||||
return a+b
|
||||
end
|
||||
|
||||
function mult(a,b)
|
||||
return a*b
|
||||
end
|
||||
|
||||
function cat(a,b)
|
||||
return tostring(a)..tostring(b)
|
||||
end
|
||||
|
||||
local function reduce(fun,a,b,...)
|
||||
if ... then
|
||||
return reduce(fun,fun(a,b),...)
|
||||
else
|
||||
return fun(a,b)
|
||||
end
|
||||
end
|
||||
|
||||
local arithmetic_sum = function (...) return reduce(add,...) end
|
||||
local factorial5 = reduce(mult,5,4,3,2,1)
|
||||
|
||||
print("Σ(1..9) : ",arithmetic_sum(table.unpack(nums)))
|
||||
print("5! : ",factorial5)
|
||||
print("cat {1..9}: ",reduce(cat,table.unpack(nums)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue