Hello,

Each time you use Add, it adds perm as a new list entry in out. It does NOT add 
the current value of perm as a new list entry in out, but rather inserts a 
reference to the list perm. The list perm is added to the list out three times, 
and by the time the function returns, perm = [1,2,3], so out consists of three 
[1,2,3].

If you want to remember previous states of a list, you'll always need to make a 
copy, because this is not done automatically when using functions like Add. Try 
using for example the ShallowCopy function.

Hope that helps.

Yours,
    Keshav

Disclaimer: the boilerplate text at the foot of this email has been added 
automatically by my mail server. This was not done by my request (rather the 
opposite) so please disregard it.



2011/1/8 Hebert Pérez-Rosés 
<hebert.pe...@gmail.com<mailto:hebert.pe...@gmail.com>>
Dear all,

I'm embarrased to say this, but I'm very puzzled by the behaviour of the Add
function. I have this simple test code:

--------------------------------------
 TestFun:= function(n)
  local perm, out, i;

  out:= [];
  perm:= [];
  for i in [1..n] do
      perm[i]:=0;
  od;
  for i in [1..n] do
      perm[i]:= i;
      Add(out, perm);
  od;
  return out;
 end;
----------------------------------

If I now make a call to Test(3), I should get [ [1,0,0], [1,2,0], [1,2,3] ],
right? However, I'm getting [ [1,2,3], [1,2,3], [1,2,3] ]. Do you get the
same thing? Can anybody explain to me what's happening here?

Thank you very much in advance,

Hebert Perez-Roses
The University of Newcastle, Australia
_______________________________________________
Forum mailing list
Forum@mail.gap-system.org<mailto:Forum@mail.gap-system.org>
http://mail.gap-system.org/mailman/listinfo/forum


________________________________
CONFIDENTIALITY: This email is intended solely for the person(s) named and may 
be confidential and/or privileged. If you are not the intended recipient, 
please delete it, notify us and do not copy, use, or disclose its content. 
Thank you.

Towards A Sustainable Earth: Print Only When Necessary
_______________________________________________
Forum mailing list
Forum@mail.gap-system.org
http://mail.gap-system.org/mailman/listinfo/forum

Reply via email to