The help page for  ?procedure has the following example.

addList := proc(a::list,b::integer)::integer;
    local x,i,s;
    description "add a list of numbers and multiply by a constant";
    x:=b;
    s:=0;
    for i in a do
        s:=s+a[i];
    end do;
    s:=s*x:
end proc:

sumList:=addList([1,2,3,4,5],2);

30

(1)

Of course it's a bug (actually a typo) here:  s:=s+a[i];   should be   s:=s+i;

A strange/funny fact is that the result is correct but, for almost any other list the call will produce an error or a wrong answer.


Please Wait...