0

I'm new to OZ Mozart, I'm trying to write a triangular sequence but the programming won't work.

declare
fun {Sequence N R}
   fun {Help I}
      if I < N
     sum = {Int.toFloat(N*(N+1)/2.0)}
     %I + 1
     case R of nil then {Append [sum] nil}
     [] H|T then sum|H|T
     end
     I+1
      end
   end
in
   {Help 0}
end

declare
{Browse {Sequence 5 nil}}

If there anything wrong with my programming? It shows error like:

%*************************** parse error ************************
%**
%** syntax error, unexpected T_end, expecting T_then
%**
%** in file "c:/Users/admin/Desktop/test (2).oz", line 11, column 6
%** ------------------ rejected (1 error)

Any idea about that? Thank you

Kun
  • 580
  • 2
  • 13

1 Answers1

0

If I well understood what a triangular sequence is, the followinf is a simple implementation. But first of all your error means that you have to use the then keyword in the if statement. Type conversion from float to int is not necessary as every number multiplied by its successor gives an odd number. This simplify the variable management. Here's what I suggest:

declare

fun {Sequence N}
   local X in
     if N>0 then
      X = (N*(N+1) div 2)
      X|{Sequence N-1}
     else nil
     end
    end
 end

{Browse {Sequence 5}} 

This is just an example, it gives a triangular sequence in reverse order, you can easily fix it according to your desire.

rok
  • 2,574
  • 3
  • 23
  • 44