hslua-examples: simplify Fibonacci Lua code
@@ -10,27 +10,21 @@ main = do
putStrLn "Calling Lua to output some Fibonacci numbers."
luaStatus <- run $ do
openlibs -- load the default Lua packages
- dostring luaProgram
+ dostring luaProgram -- load and run the program
putStrLn ("Lua finished with status '" ++ show luaStatus ++ "'.")
--- | The Lua program. It uses a coroutine to generate Fibonacci numbers,
--- printing the first 11 of them.
+-- | The Lua program. It uses a simple loop to calculate and print the
+-- first 11 Fibonacci numbers.
--
-- Note that we are using the /OverloadedStrings/ extension. The
-- @IsString@ instance for ByteString behaves weirdly with non-ASCII
-- characters, but we don't use any here.
luaProgram :: B.ByteString
luaProgram = B.concat
- [ "local fibs = coroutine.create(function ()\n"
- , " local a, b = 0, 1\n"
- , " while true do\n"
- , " coroutine.yield(a)\n"
- , " a, b = b, a + b\n"
- , " end\n"
- , "end)\n"
- , "\n"
+ [ "local a, b = 0, 1\n"
, "for i = 0, 10 do\n"
- , " print(i, select(2, coroutine.resume(fibs)))\n"
+ , " print(i, a)\n"
+ , " a, b = b, a + b\n"
, "end\n"
]
