lst := [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]: r1,r2:= lst[1],lst[1]: for i from 2 to numelems(lst) do if lst[i] - lst[i-1] = 1 then #consecutive r2 := lst[i]: else #break printf(piecewise(r2-r1=1, "%d,%d,", r2-r1>1,"%d-%d,", "%d,"), r1, r2): r1,r2:= lst[i],lst[i]: fi: od: printf(piecewise(r2-r1=1, "%d,%d", r2-r1>1,"%d-%d", "%d"), r1, r2):