Playing with numbers: Number range creator

Submitted by Mercutio on Tue, 2011-05-24 19:17

In this post we created a function that will change a set of number ranges into a simple list of numbers. But what if we wanted to do it the other way around!?

So, if we have the following list:

5 7 8 9 3 6 12 13 14

How do we get to:

3 5-9 12-14

One way of doing this, as taken from M*U*S*H's snippet machine in #0, is the following:

&ATTR1 OBJ=trim(fold(me/ATTR2,unique(sort(%0,n))))
&ATTR2 OBJ=if(eq(%1,inc(last(last(%0),-))),strcat(extract(%0,1,dec(words(%0))),%b,first(last(%0),-),-,%1),%0 %1)

How would you do this?