[GAP Forum] Lists and sub-functions

mim_ at op.pl mim_ at op.pl
Tue Nov 20 11:36:32 GMT 2007


Hello,

I have following questions regarding lists.

1. Is it safe to pass lists as parameters to functions ?
2. Is it safe to return lists from functions ?

And also additional question.

3. Is it allowed to define sub-functions inside a function ? I have seen such example in lib/alglie.gi - there is subfunction in RootSystem function. But when I tried I see error.

Regarding first 2 questions. Since assignment
a:=b; 
for list do not create new list, I assume that list is really a pointer (however this is never mentioned in GAP documentation). So if I declare local variable in function, create list inside function and return it. Then pointer to a list is returned but local memory of the function can be overwritten now. So my list is not safe !?

Here is example
create_mat:=function (n)
  local i, m;
  m:=[];
  for i in [1..n] do
    Add(m, [i..i+n-1]);
  od;
  return m;
end;;

a:= create_mat(5);    
# what is it <a> now ? Is it matrix  (list) or just pointer to piece of memory which can be overwritten ?

I am asking because I am receiving strange errors when working with matrices. The error is saying that boolean is found where list is expected !?
For interested persons I attach my code in example.txt file.
So far everything worked OK (almost) with my matrix GAP code.

Regards,
Marek Mitros


-------------- next part --------------
# For matrix m returns coordinates [x,y] of all elements equal to <a> e.g. a=1.
# Assuming that in each row there is only one element equal to 1
mat_ones:=function(m,a)
  local i,j, l,x,y;
  l:=[];
  for i in [1..Length(m)] do
     if ForAny(m[i], x->x=a) then
        Add(l, [i, Position(m[i], a)]);
     fi;
  od;
  return l;

end;;

# received pair of numbers from range 1..24, returned 0..3
group24:=function(p)
  local s;
  #Print(p, " ", p[1]," ", p[2], "\n");
  s:=Set([Int(p[1]/8),Int(p[2]/8)]);
  if Length(s)=1 then
     return 0;
  else # 01, 02, 12
     return s[1]+s[2];
  fi;
end;;


# here is ..... piece of code inside another function
# mm is matrix 28*28

       ll:=mat_ones(mm,1);   # something is wrong here
       #pp:=First(ll, x->IsSubset([1..24],x));  #pair of indexes from set [1..24]
       pp:=ll[Position(List(ll, x->IsSubset([1..24],x)), true)];
       #Print(pp, IsList(pp),"\n");
       f[3]:=group24(pp);
       
# I see error message that pp is not list but boolean !?



More information about the Forum mailing list