Lua 递归 vs Python 递归

我写了一个简单的程序来学习 Lua 的基础知识,但它创建了一个堆栈溢出。然而,在 Python 中用完全相同的代码则运行得非常完美。 Lua 代码:

function tprint (tbl, indent)
  if not indent then indent = 0 end
  for k, v in pairs(tbl) do
    formatting = string.rep("  ", indent) .. k .. ": "
    if type(v) == "table" then
      print(formatting)
      tprint(v, indent+1)
    else
      print(formatting .. v)
    end
  end
end

function encryptNumber(num)
    return 3 * num + 2
end

function decryptNumber(num)
    return (num - 2) / 3
end

function encryptString(str)
    enstr = ""
    for i = 1,string.len(str) do
        enstr = enstr..string.char((string.byte(str,i) + 19) % 255)
    end
    return enstr
end

function decryptString(str)
    destr = ""
    for i = 1,string.len(str) do
        destr = destr..string.char((string.byte(str,i) - 19) % 255)
    end
    return destr
end

function encryptTable(t)
    newt = {}
    for i,j in pairs(t) do
        newi = encrypt(i)
        newj = encrypt(j)
        newt[newi] = newj
    end
    return newt
end

function decryptTable(t)
    newt = {}
    for i,j in pairs(t) do
        newi = decrypt(i)
        newj = decrypt(j)
        newt[newi] = newj
    end
    return newt
end

function encrypt(obj)
    if type(obj) == "number" then
        return encryptNumber(obj)
    end
    if type(obj) == "string" then
        return encryptString(obj)
    end
    if type(obj) == "table" then
        retval = encryptTable(obj)
        return retval
    end
end

function decrypt(obj)
    if type(obj) == "number" then
        return decryptNumber(obj)
    end
    if type(obj) == "string" then
        return decryptString(obj)
    end
    if type(obj) == "table" then
        retval = decryptTable(obj)
        return retval
    end
end

test = {1, {'a'}, '3'}
tprint(test)
tprint(encrypt(test))
tprint(decrypt(encrypt(test)))

完全相同的 Python 代码则运行得非常完美:

def encryptNumber(num):
    return 3 * num + 2

def decryptNumber(num):
    return (num - 2) / 3

def encryptString(str):
    enstr = ""
    for i in range(len(str)):
        enstr = enstr + chr((ord(str[i]) + 19) % 255)
    return enstr

def decryptString(str):
    destr = ""
    for i in range(len(str)):
        destr = destr + chr((ord(str[i]) - 19) % 255)
    return destr

def encryptTable(t):
    newt = {}
    for i in t:
        newi = encrypt(i)
        newj = encrypt(t[i])
        newt[newi] = newj
    return newt

def decryptTable(t):
    newt = {}
    for i in t:
        newi = decrypt(i)
        newj = decrypt(t[i])
        newt[newi] = newj
    return newt

def encrypt(obj):
    if type(obj) == int:
        return encryptNumber(obj)

    if type(obj) == str:
        return encryptString(obj)

    if type(obj) == dict:
        return encryptTable(obj)

def decrypt(obj):
    if type(obj) == int:
        return decryptNumber(obj)

    if type(obj) == str:
        return decryptString(obj)

    if type(obj) == dict:
        return decryptTable(obj)

test = {2: {1:2}, 1: {'a': {'c':'d'}}}
print test
print encrypt(test)
print decrypt(encrypt(test))

Lua 递归和 Python 递归有区别,并且造成了这个问题吗?或者是逻辑错误所致?

点赞