kill(all)$

peter.vlasschaert@gmail.com,09/04/2018,version 1.0

programming: block 
=================
=================

syntax : block([var_1,var_2,...,var_k],statement_1,statement_2,...,statement_j)

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

use : return 

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

p1:block([x],x:1,n:x+n,return(n))$
n;
x;

why this result : x  not known the outside block statement,only 'n' changes
                               n  ?  => x:1 => n : n+1 
--------------------------------------------------------------------------------------------------------------------
rem : $ no result for 'p1'
rem : var_1              :     x
rem : statement_1  :     x:1
rem : statement_2  :     n:x+n

p2:block([y],y:1,n:y+n,return(n));
n;
y;

why this result :  y not known outside the block statement ,from 'p1' follows  n : (n+1) 
                                n ? =>  y:1 => n:1+(n+1) => n:n+2

p3:block([z],z:1,m:z+m,return([m,z]));
p3;
p3[1];
p3[2];
 m;
 z;

why this result : 'p3' result list, p3[1] first element from the list 'p3'
                                                        , p3[2] second element from the list 'p3'
                                z not known outside the block statement  
                                m ? => z:1 => m:1+m

p4:block ([v],v:0,for count :1 thru 10 do
                   v:v+count,return(v));
count;

p5: 10*(10+1)/2;

why this  result  :  n(n+1)/2 = 1+2+3+..+10 = 55       , sum of n integers.                            

for  : inside  ' block statement '
=========================

p6:block ([w],w:0,for count :1 thru 10 do
                   w:w+count,return([w,count]));
      count;

why this result : value count , not known outside the ' block statement '

p7: f(m):=block([z],m:z+m)$
z;
p8:f(2);

why this result : input m:2 
                               m ? =>  z + ' input m:2' => m:z+2

if : inside 'block statement '
======================

p9:g(n):=block([w],if not integerp(n) or n < 0 then
                          return([n,"is not a nonnegative integer"])
                          else 
                          w:w+n,
                          return([n,w])
                          )$

rem  :  string =  "........"   

w;

g(-2);
g(2);

rem : integerp

integerp(2);
integerp(-2);
integerp(-2.5);

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

use : go(tag)
rem : only be used in 'block statement '

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

math function
============

ned:https://nl.wikipedia.org/wiki/Faculteit_(wiskunde)
eng:https://en.wikipedia.org/wiki/Factorial

my factorial : 'Maxima'
==========

p10:h(n):=block([x],x:1,test,
                                                if n = 0 then 
                                                 return(x),
                                                 x:x*n,
                                                 n:n-1,
                                      go(test))$
x;

n:n-1 => n:n+1  , infinite loop which never ends.
(otherwise : use do loop ,with if statement to exit do loop)

h(0);

h(1);
h(2);
h(3);
h(4);

same result as above.

build in factorial :  'Maxima'
==============

1!;
2!;
3!;
4!;
0!;

rem : h(0) = 0 !
           h(n)= n !,  n: integer