Bill Allombert on Mon, 08 Feb 2016 20:31:12 +0100


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

Re: addhelp


On Mon, Feb 08, 2016 at 08:08:24PM +0100, Josef Eschgfaeller wrote:
> Bill Allombert wrote:
> 
> > One dirty way to do it in GP is:
> > arity(f)=component(f,2)[1]
> 
> Many thanks - it seems that one
> can obtain in this way also the
> implicit arity (see example):
> 
>  If I understand well,
> component(f,2)[2] is always the
> value of the first argument, if this
> is optional. 

No, only if the first argument is a small positive integer.

? f(x,a=-11)=x+a
%17 = (x,a=-11)->x+a
? component(f,2)[2]
%18 = 1

I would suggest something simpler:

f(v=0) = 
{ 
  if (v===0,return(3));
  my ([x,y,z]=v); x+y+z
}

elaborate (f) = {my (n,e); n=f(); e=f([2,5,1]); n*e}

t=elaborate(f)
print(t) \\ 24

Cheers,
Bill.