Quantcast

Error in PiL3 in Listing 12.1 (page 121)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error in PiL3 in Listing 12.1 (page 121)

Egor Skriptunoff-2
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)
      end
   end
   -- 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."

-- Egor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in PiL3 in Listing 12.1 (page 121)

Tomas Guisasola Gorham
Hi Egor

> 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)
>       end
>    end
>    -- The rest of the code is exactly the same as in the book
I test your code but it seems it does not handle all cases at all:

Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio

> function quote(s)
>>     local n = -1
>>     for w in string.gmatch(s, "]([]=]*)]") do
>>         for e in string.gmatch(w, "=*") do
>>             n = math.max(n, #e)
>>         end
>>     end
>>     local eq = string.rep("=", n+1)
>>     return string.format(" [%s[\n%s]%s] ", eq, s, eq)
>> end
> print(quote"]=]==")
  [==[
]=]==]==]
>

Regards,
Tomás

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in PiL3 in Listing 12.1 (page 121)

Egor Skriptunoff-2
On Sun, Jul 5, 2015 at 3:34 PM, tomas <[hidden email]> wrote:

I test your code but it seems it does not handle all cases at all:
print(quote"]=]==")
 [==[
]=]==]==]

Oops!  Indeed...   My bad.
This error can be fixed by appending a closing bracket to the input string before processing:

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)
      end
   end
   -- The rest of the code is exactly the same as in the book


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in PiL3 in Listing 12.1 (page 121)

Egor Skriptunoff-2
In reply to this post by Egor Skriptunoff-2
On Sun, Jul 5, 2015 at 2:56 AM, Egor Skriptunoff wrote:
Function quote() gives incorrect result for some input strings, for example "]]=]"
BTW, PiL2 contains correct (but not optimal) code for this function.
 

It seems that PiL4 has an error in the same function too.
(Figure 15.1. Quoting arbitrary literal strings)
Now quote("]]") gives wrong result.
Loading...