Re: Homogeneous sequence

new topic     » goto parent     » topic index » view thread      » older message » newer message

Juergen Luethje wrote:
> 
> Ricardo Forno wrote:
> 
> > Hi all.
> > While reading this thread, I browsed my General Functions package and saw
> > an obvious mistake in the example for the Structure function, which does
> > a very similar thing to one of the functions discussed here.
> > For now, I don't plan to post a correction, because the mistake is so
> > obvious.
> > Regards.
> 
> Hello Ricardo,
> 
> this reminds me ...
> When looking for the solution of a general problem, I actually should
> firstly look into your great General Functions package.
> 
> Regards,
>    Juergen

Hi, Juergen

This works, I think, though I'd rather have an iterative solution.

type seq_of_atom_or_empty(sequence s)
    if equal(s, {}) then
        return 1
    end if
    for i = 1 to length(s) do
        if sequence(s[i]) then
            return 0
        end if
    end for
    return 1
end type

function object_structure_2(object x)
    if atom(x) then
        return 1
    elsif seq_of_atom_or_empty(x) then
        return {}
    end if

    for i = 1 to length(x) do
        x[i] = object_structure_2(x[i])
    end for
    return x
end function

global type homogeneous_sequence_2 (object x)
   object struc

   if atom(x) then
      return 0
   end if

   if length(x) = 0 then
      return 1                  -- ?
   end if

   struc = object_structure_2(x[1])
   for i = 2 to length(x) do
      if not equal(struc, object_structure_2(x[i])) then
         return 0
      end if
   end for
   
   return 1
end type


-- Demo:
object a, b, c
a = {3,{3 ,5,{5,2},4}}
b = {4,{5 ,1,{9,2},4}}
c = {4,{{},1,{9,2},4}}

-- ? object_structure(a)
-- ? object_structure(b)
-- ? object_structure(c)
-- ? homogeneous_sequence({a,b})
-- ? homogeneous_sequence({a,c})
-- ? homogeneous_sequence({b,c})
? object_structure_2(a)
? object_structure_2(b)
? object_structure_2(c)
? homogeneous_sequence_2({a,b})
? homogeneous_sequence_2({a,c})
? homogeneous_sequence_2({b,c})


Bob

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu