Extending assignment Was: Functions with any number of arguments
Subject: Extending assignment Was: Functions with any number of arguments
From: Alexander Klimov
Date: Thu, 19 Jan 2012 18:17:00 +0200
On Sun, 15 Jan 2012, Aleksas Domarkas wrote:
> How in Maxima define functions with any number of arguments ?
>
> For example, i need function MIN(arg1,arg2, ...)
> MIN(2) => 2
> MIN(3,4) =>3
> MIN(2/3,1/2,sqrt(2)/2) => 1/2
MIN([args]) := block([m:first(args)],
for i in rest(args) do m:min(m,i),
m);
> Other example. How define G with following properties:
> G(n) => gamma(n)
> G(5) => 24
> G(a, x) => gamma_incomplete (a, x)
> G(a, z1, z2) => gamma_incomplete_generalized (a, z1, z2)
Use "length(args)" and "if <cond_1> then <expr_1> elseif ..."
What I wonder, is whether there is a more efficient way to check that
a list has empty tail than evaluate its length: I guess "empty" should
be a built-in function:
empty(l) := block([simp:false], ?null(?cdr(l)));
list_min(l) := block([f:first(l),r:rest(l)],
if empty(r) then f else min(f, list_min(r)));
MIN([args]) := list_min(args);
It would also be nice if "[a,b]:[1]" were not an error but instead
assigns false (NIL) to b. This would allow to do
[a,b,c]:args;
if c then /* 3 args */
elseif b then /* 2 args */
else ...
--
Regards,
ASK