[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Error in PiL3 in Listing 12.1 (page 121)
- From: Egor Skriptunoff <egor.skriptunoff@...>
- Date: Sun, 5 Jul 2015 02:56:02 +0300
Function quote() gives incorrect result for some input strings, for example "]]=]"
BTW, PiL2 contains correct (but not optimal) code for this function.
A correct code may look like the following:
function quote (s)
-- find maximum length of sequences of equal signs
local n = -1
for w in string.gmatch(s, "](=]*)]") do
for e in string.gmatch(w, "=*") do
n = math.max(n, #e)
-- The rest of the code is exactly the same as in the book
Probably, it is more important for code to be simple and beginner-friendly than correct.
Instead of modifying code of Listing 12.1 this issue may be solved by introducing an exercise: "Find an input string on which function quote() fails to get correct result. Rewrite the function to fix this error."