lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Apr 17, 2012 at 12:43 PM, Joseph Manning <manning@cs.ucc.ie> wrote:
> On 2012-Apr-17 (Tue) at 11:18 (-0500), Coda Highland wrote:
>
>>> function table.issequence(t)
>>>     for i = 1, #t do
>>>         if t[i] == nil then return false end
>>>     end
>>>     return true
>>> end
>
> Counterexample:
>
>   $ cat test.lua
>   function issequence(t)
>       for i = 1, #t do
>           if t[i] == nil then return false end
>       end
>       return true
>   end
>
>   t      = { 1, 2 }
>   t[ 2 ] = nil
>   t[ 3 ] = 3
>
>   print( t[ 1 ], t[ 2 ], t[ 3 ], issequence( t ) )
>
>   $ lua -v test.lua
>   Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
>   1       nil     3       true
>
> Joseph

That's not a counterexample, that's just a special case. In this case,
this test is reporting that t IS a sequence, and #t is returning the
length of it -- what it isn't reporting is that there are also
non-sequence parts of t. If you want THAT test, of course, you can
code for it too.

/s/ Adam