loops in maxima



maxima-bounces at math.utexas.edu wrote on 12/06/2006 02:24:30 PM:

> Hi, i'm a beginner in maxima and i'm trying to implement a loop such as 
in C:
> 
> while(x = 0)
> {
>   i1;
>   i2;
>   ...
>   in;
> }
> 
> 
> The problem is that i can't write a loop of more than one instruction 
inside. 
> Can you write any example, please?
> Thank you.

Sure--to make a compound statement, enclose the statements by parenthesis
and separate the statements with commas. Example:

bits(n) := block([l : []],
   while n # 0 do (
      l : endcons(mod(n, 2), l),
      n : floor(n / 2)),
    l);

(%i9) bits(n) := block([l : []],
   while n # 0 do (
      l : endcons(mod(n, 2), l),
      n : floor(n / 2)),
    l)$

(%i10) bits(28);
(%o10) [0,0,1,1,1]
(%i11) bits(2);
(%o11) [0,1]
(%i12) bits(3);
(%o12) [1,1]
(%i13) bits(4);
(%o13) [0,0,1]

Let us know if you have more questions.

Barton