lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> text/plain      /home/roberto/lua/testes/bwk/lua-all

Sorry about that. Let me try again...

-- Roberto

=====================================================================
  Suite of Lua scripts for "Experiments with Scripting and User-Interface
  Languages", by Brian W. Kernighan & Christopher J. Van Wyk 
  (http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html).

  To change `lim', call "lua lim=XXXX prog.lua"
  For instance, to compute ack(3,6), call
  > lua lim=6 ack.lua
=====================================================================
-- ack.lua
-- ackerman function

lim = tonumber(lim) or 7

function ack(m,  n)
  if (m == 0) then
    return n+1
  elseif (n == 0) then
    return ack(m-1, 1)
  else
    return ack(m-1, ack(m, n-1))
  end
end 

print(ack(3,lim))


=====================================================================
-- array1.lua
-- test for arrays

local n = tonumber(lim) or 100000

local x, y = {}, {}
local i = 1
while i<=n do
  x[i] = i
  i = i+1
end

local j = n
while j>=1 do
  y[j] = x[j]
  j = j-1
end


=====================================================================
-- assoc.lua
-- test for associative arrays
-- This test is very bad for Lua :-(. It takes much more time handling
-- strings (format and convertion from number to string) than in the tables

local n = tonumber(lim) or 10000

local i=1
local X={}
while i<=n do
  X[format("%x", i)] = i
  i = i+1
end

local c = 0

i = n
while i>0 do
  if X[i..''] then c = c+1 end
  i = i-1
end
print(c)
  

=====================================================================
-- cat.lua
-- cat: copies a file from stdin to stdout

local line = read()
while line do
  write(line, "\n")
  line = read()
end


=====================================================================
-- cat1.lua
-- cat: copies a file from stdin to stdout
-- reads the whole file at once

write(read"*a")


=====================================================================
-- string.lua
-- test for string manipulation

local n = tonumber(lim) or 100000

local s

local j = 1
while j<=10 do
  s = "abcdef"
  while strlen(s) <= n do
    s = "123"..s.."456"..s.."789"
    s = strsub(s, strlen(s)/2) .. strsub(s, 1, strlen(s)/2)
  end
  j = j+1
end

print(strlen(s))


=====================================================================
-- sum1.lua
-- add numbers in a file

local s = 0
local n = read"*n"    -- reads a number
while n do
  s = s+n
  n = read"*n"
end
print(s)


=====================================================================
-- sumloop.lua
-- test for basic loops

local n = tonumber(lim) or 1000000
local s = 0
local i = 0
while i<n do
  s=s+1
  i=i+1
end
print(s)


=====================================================================
-- tail.lua
-- invert the lines of a file

local lines = {}
local nl = 0
local line = read()
while line do
  nl = nl+1
  lines[nl] = line
  line = read()
end

while nl>=1 do
  write(lines[nl], "\n")
  nl=nl-1
end




=====================================================================
-- wc.lua
-- print the number of bytes, words, and lines of a file

local cc,lc,wc = 0,0,0
local line = read()
while line do
  cc = cc+strlen(line)
  lc = lc+1
  local _,t = gsub(line, "%S+", "")   -- count words in the line
  wc = wc+t
  line = read()
end

print(lc,wc,cc+lc)   -- adds `lc' to `cc' to include newlines */
  


=====================================================================
-- wc1.lua
-- print the number of bytes, words, and lines of a file
-- read the whole file at once

local t = read"*a"
local cc = strlen(t)
local _,lc = gsub(t, "\n", "\n") 
local _,wc = gsub(t, "%S+", "")
print(lc,wc,cc)