kill(all)$

maxima : programming block -,compound (statement),program,symbolic,version 1.0
peter.vlasschaert@gmail.com,09/06/2022

use : simpsum,listofvars,listdummyvars,showratvars,facsum,factor,endcons,
         block statement (return),compound statements.

================================================

given : 1+2+3+......+n = ? , n = integer , n>0

================================================

→  manual 
n=1 →1 :1
n=2 →1+2= 3
n=3 →1+2+3 =6.
----------------------------
n=i  →1+2+3+    +i

================================================

→ symbolic

1e method : symbolic 'sum'
------------------------------------

p1:'sum(i,i,1,n) =ev(sum(i,i,1,n),simpsum);

info : variables

rem : i = dummy.

listdummyvars;/*default :option*/

p11:listofvars(p1);

→ option changes : true,false

listdummyvars:false;/* no dummy variables included */
p11aa:listofvars(p1);

p22:showratvars(p1);

p1a:lhs(p1) = facsum(rhs(p1));
p1b:lhs(p1) = factor(rhs(p1));

→ numerical

nu:5;/*integer*/

2e method : program , script
-------------------------------------

som : 0$  /*init sum */
for i:1 thru nu do
(
som:som + i,
a[i]:som,                                  /*lhs(p1)*/
b[i]:subst([n=i],rhs(p1b))        /*rhs(p1)*/
)$
makelist([a[i],b[i]],i,1,nu);

[som,i];

kill(som,i)$

[som,i];

3e method : compound statement.
---------------------------------------------

→ one line code:

(som : 0,le:[],for i:1 thru nu do(
    le:endcons([som:som+i,((i+1)*i)/2],le)),le
    );

rem : le:[],empty list
endcons : new list with expression as last element,preceded by 
                 the elememts of  list

4e method : block statement.
--------------------------------------

somnum(ii):=block([i,som:0],
for i:1 thru nu 
                       do(
                   som:som + i,
                   a[i]:som,                                  /*lhs(p1)*/
                   b[i]:subst([n=i],rhs(p1b))        /*rhs(p1)*/
                            ),
   return( makelist([a[i],b[i]],i,1,nu))
    )$

→ optimize : code 

grind(somnum)$

syntax :
namefun(arg,[]):=block([local,start variables],statements,return(variables))
rem : arg = arguments. ("variables ,list")

→ variables,dummy

[som,i];

rem :

======================================================
local variables          :i
start variables : som:0  
======================================================

somnum(nu);

---------------------------------------------------------------------------------------------------------------------