lua-users home
lua-l archive

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


On Sunday 30 January 2005 9:53 pm, PA wrote:
> On Jan 31, 2005, at 02:44, Javier Guerra wrote:
> > this is the 'same leaves' problem, there are several interesting
> > solutions.
> > it's used in functional programming classes because there's no simple
> > solution without coroutines and/or lazy evaluation
>
> Care to elaborate in more details?

slight mistake: the name is 'Same Fringe Problem' (see 
http://c2.com/cgi/wiki?SameFringeProblem).  the general case is to compare 
two trees and determine if they have the same leaves, even if the branching 
structure is different.  essentialy, the simpler solutions use an iterator 
that enumerates the leaves in left-right order, and then compare the results 
like lists.  the catch is that good tree iterators aren't easy to write 
without coroutines

but i think you're treating tables mostly as arrays, that's a far easier 
problem.  even if they have tables as elements, a recursive function would be 
enough

> > that seems (to me) like a square peg in a round hole.
>
> I like that. This will be LUObject's official motto from now on.
> Thanks! :)

you're welcome!  but when you find round pegs, you'll have much more pleasant 
insertions ;-)

> > if you don't try to define everything in that LUObject
>
> Sigh... let me rephrase it:
>
> local aKey = { blue, white, read }
> local aValue = { 1, 2, 3 }
> local aTable = { aKey = aValue }
> local anotherkeyTable = { blue, white, read }
>
> aTable[ anotherkeyTable ]
>
> The key equality is based on the table content. How?

not easy, in fact.  you need __index and __newindex metamethods, of course. 
and lose the constructor syntax.  (because values have to be added after 
attaching the metatable)

btw, remember that 
   local aTable = { aKey = aValue }
is sugar for
   local aTable = {}
   aTable.aKey = aValue
and that's sugar for
   aTable ["aKey"] = aValue

i think you'd want something like
   aTable [aKey] = aValue
so you have to write
   local aTable = { [aKey] = aValue }

but that won't work for you, because of the equality definition.

a crude, but working hack would be to write a function that returns a string 
that would be the same for any identical table.  similar in concept to a hash 
code, but without collitions and the performance issues would be managed 
transparently by the internal string hashing.

maybe use a persistence library, to serialize a any value to a string and use 
the string as the key

-- 
Javier

Attachment: pgpe3CR8AlTLE.pgp
Description: PGP signature