lua-users home
lua-l archive

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


On 8/9/05, Alain <alainm@pobox.com> wrote:
> You certainly acheived one thing: It has been a long time since I look a
> factorial function and cannot in any way understand it  ;-)

Is the _only_ reason that you `cannot understand'
the factorial function (coming with the Lua interpreter)
that I have written some tokens in it in a new way?

Then you will have no difficulties in guessing what the
following Lua program does -- after all, it has a truly
orthodox syntax.  Well, if you cannot, within an hour
or two, tell what it is, just run it :)

function f1()
  return coroutine.wrap(
    function()
      local x = 0
      while true do
        x = x+1
        coroutine.yield(x)
      end
    end
  ) 
end

function f2(f)
  return coroutine.wrap(
    function()
      local x = 0
      while true do
        x = x+f()
        coroutine.yield(x)
      end
    end
  )
end

function f3(f)
  return coroutine.wrap(
    function()
      local g,h = f1(),f2(f1())      
      local x z = h()
      while true do
        local y = g()
        if y==z
          then z = h()
          else coroutine.yield(x)
        end
        x = f()
      end
    end
  )
end

n = 10^10
f = f1()
while true do
  local x = f()
  if x>n then break end
  print(x)
  f = f2(f3(f))
end