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


INT FUNC FindRange(INT ARRAY a INT len,start)
  INT count
 
  count=1
  WHILE start<len-1
  DO
    IF a(start)+1#a(start+1) THEN
      EXIT
    FI
    start==+1
    count==+1
  OD
RETURN (count)
 
PROC Append(CHAR ARRAY text,suffix)
  BYTE POINTER srcPtr,dstPtr
  BYTE len
 
  len=suffix(0)
  IF text(0)+len>255 THEN
    len=255-text(0)
  FI
  IF len THEN
    srcPtr=suffix+1
    dstPtr=text+text(0)+1
    MoveBlock(dstPtr,srcPtr,len)
    text(0)==+suffix(0)
  FI
RETURN
 
PROC RangeToStr(INT ARRAY a INT len CHAR ARRAY res)
  INT i,count
  CHAR ARRAY tmp(10)
 
  i=0
  res(0)=0
  WHILE i<len
  DO
    count=FindRange(a,len,i)
    StrI(a(i),tmp) Append(res,tmp)
    IF count=2 THEN
      Append(res,",")
      StrI(a(i+1),tmp) Append(res,tmp)
    ELSEIF count>2 THEN
      Append(res,"-")
      StrI(a(i+count-1),tmp) Append(res,tmp)
    FI
    i==+count
    IF i<len THEN
      Append(res,",")
    FI
  OD
RETURN
 
PROC Main()
  INT ARRAY a=[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]
  INT ARRAY b=[65530 65533 65534 65535
    0 1 3 4 5 7 8 9 10 11 14 15 17 18 19 20]
  CHAR ARRAY res(256)
 
  RangeToStr(a,33,res)
  PrintE(res) PutE()
  RangeToStr(b,20,res)
  PrintE(res)
RETURN