A format for expressing an ordered list of integers is to use a comma separated list of either

 individual integers
 Or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. (The range includes all integers in the interval including both endpoints)
 The range syntax is to be used only for, and for every range that expands to more than two values.
Example

The list of integers:

 -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20
Is accurately expressed by the range expression:

 -6,-3-1,3-5,7-11,14,15,17-20
(And vice-versa).

Task

 Create a function that takes a list of integers in increasing order and returns a correctly formatted string in the range format. 
 Use the function to compute and print the range formatted version of the following ordered list of integers. (The correct answer is: 0-2,4,6-8,11,12,14-25,27-33,35-39).


    0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
   25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
   37, 38, 39

 Show the output of your program.


Related task

   Range expansion


 
MODULE RangeExtraction;
IMPORT Out;
 
PROCEDURE Range(s: ARRAY OF INTEGER);
VAR
	i,j: INTEGER;
 
	PROCEDURE Emit(sep: CHAR);
	BEGIN
		IF i > 2 THEN
			Out.Int(s[j],3);Out.Char('-');Out.Int(s[j + i - 1],3);Out.Char(sep);
			INC(j,i)
		ELSE
			Out.Int(s[j],3);Out.Char(sep);
			INC(j)
		END;
	END Emit;
 
BEGIN
	j := 0;i := -1;
	LOOP
		INC(i);
		IF j + i >= LEN(s) THEN 
			Emit(0AX);
			EXIT 
		ELSIF s[j + i] # (s[j] + i) THEN
			Emit(',');
			i := 0;
		END
	END
END Range;
 
VAR
	seq0: ARRAY 33 OF INTEGER;
	seq1: ARRAY 20 OF INTEGER;
BEGIN
	seq0[0] := 0;
	seq0[1] := 1;
	seq0[2] := 2;
	seq0[3] := 4;
	seq0[4] := 6;
	seq0[5] := 7;
	seq0[6] := 8;
	seq0[7] := 11;
	seq0[8] := 12;
	seq0[9] := 14;
	seq0[10] := 15;
	seq0[11] := 16;
	seq0[12] := 17;
	seq0[13] := 18;
	seq0[14] := 19;
	seq0[15] := 20;
	seq0[16] := 21;
	seq0[17] := 22;
	seq0[18] := 23;
	seq0[19] := 24;
	seq0[20] := 25;
	seq0[21] := 27;
	seq0[22] := 28;
	seq0[23] := 29;
	seq0[24] := 30;
	seq0[25] := 31;
	seq0[26] := 32;
	seq0[27] := 33;
	seq0[28] := 35;
	seq0[29] := 36;
	seq0[30] := 37;
	seq0[31] := 38;
	seq0[32] := 39;
	Range(seq0);
	seq1[0] := -6;
	seq1[1] := -3;
	seq1[2] := -2;
	seq1[3] := -1;
	seq1[4] := 0;
	seq1[5] := 1;
	seq1[6] := 3;
	seq1[7] := 4;
	seq1[8] := 5;
	seq1[9] := 7;
	seq1[10] := 8;
	seq1[11] := 9;
	seq1[12] := 10;
	seq1[13] := 11;
	seq1[14] := 14;
	seq1[15] := 15;
	seq1[16] := 17;
	seq1[17] := 18;
	seq1[18] := 19;
	seq1[19] := 20;
	Range(seq1)
END RangeExtraction.
 