--[[ Copyright (c) 2012 Igor Bogomazov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. No conditional branches: 1. no hashes (as far as possible in Lua) 2. no for/while/until loops 3. no local operations 4. no exceptions Idea: http://www.99-bottles-of-beer.net/ --]] -- return: 0 if 0 given or 1 otherwise local function norm(n) return #( ("1"):rep(n / n) ) end; -- bottle phrase local function bottle(n) return "" .. ("no more"):rep( -(norm(n) - 1) ) .. tostring(n):rep(norm(n)) .. " bottle" .. ("s"):rep( norm(n - 1) ) .. " of beer" ; end; -- the second row in verses local function second(n) return "" .. "Take one down and pass it around, " .. bottle(n - 1) .. " on the wall.\n" ; end; -- the final row in the song local function final(n, init) return "" .. "Go to the store and buy some more, " .. bottle(init) .. " on the wall.\n" ; end; -- capitalize the first letter local function capital(s) return s:sub(1, 1):upper() .. s:sub(2) end; -- print out verse about `n` bottles local function verse(n, init) io.stdout:write( capital(bottle(n)), " on the wall, ", bottle(n), ".\n" ); io.stdout:write( select(norm(n) + 1, final, second)(n, init) ); return io.stdout:write "\n"; end; -- print out song local function song(n, init) verse(n, init); return select(norm(n) + 1, function() end, song)(n - 1, init); end; -- entry point local function main(n) return song(n, n) end; return main(99);