[Question]
  [
Given an unsorted list of unique, positive integers, output the shortest list of the longest possible ranges of sequential integers.
# INPUT
* An unsorted list of unique, positive integers
	+ e.g. `9 13 3 11 8 4 10 15`
* Input can be taken from any one of the following:
	+ `stdin`
	+ command-line arguments
	+ function arguments
# OUTPUT
* An ordered list of ranges or individual values printed on one line to stdout or your language's closest similar output.
	+ If two or more sequential integers (sequential by value, not by location in the list) are present, they will be denoted as an inclusive range using -, e.g. `8-11`
	+ All other integers are simply printed with no other notation
	+ A single space will delimit the output
* Numbers not present in the input should not be in the output, e.g. `3 5 6` cannot be shortened to `3-6` because `4` is not present
# EXAMPLES
**Successful:**
```
 IN> 9 13 3 11 8 4 10 15 6
OUT> 3-4 6 8-11 13 15
 IN> 11 10 6 9 13 8 3 4 15
OUT> 3-4 6 8-11 13 15
 IN> 5 8 3 2 6 4 7 1
OUT> 1-8
 IN> 5 3 7 1 9
OUT> 1 3 5 7 9
```
**Wrong:**
```
 IN> 9 13 3 11 8 4 10 15
OUT> 3-15
```
Range contains values not in the input
```
 IN> 9 13 3 11 8 4 10 15
OUT> 3 4 8 9 10 11 13 15
```
All sequential values should be represented as a range
```
 IN> 9 13 3 11 8 4 10 15
OUT> 3-4 8-9 10-11 13 15
```
Divided range, `8-9` and `10-11` should be `8-11`
```
 IN> 9 13 3 11 8 4 10 15
OUT> 8-9 13 10-11 3-4 15
```
Output not ordered correctly
# RULES
* [Standard loopholes are disallowed](http://meta.codegolf.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default)
* If your language has a function to do this it's not allowed
* You may write a full program, or a function
* trailing whitespace doesn't matter
# SCORING
* Least bytes wins
  
[Answer]
# Python 2, ~~123~~ 120 bytes
```
N=sorted(map(int,raw_input().split(' ')));print(''.join((''if n+1in N else'-'+`n`)if n-1in N else' '+`n`for n in N)[1:])
```
If the input can be a list as a function argument then (thanks mbomb007 and xnor for the conditionals)
# ~~93~~ ~~90~~ 81 bytes
```
def f(N):print''.join((' '+`n`,`-n`*-~-(n+1in N))[n-1in N]for n in sorted(N))[1:]
```
(77 bytes if leading whitespace is acceptable - drop the final `[1:]`)
[Answer]
# JavaScript (ES6): ~~171~~ ~~154~~ 140 ~~137~~ bytes
Thanks edc65 and vihan1086 for the tips! ~~`[...n]` is very nice~~ but it doesn't work in these cases due to multi-digit numbers.
```
f=n=>{s=e=o='';n.split` `.map(Number).sort((a,b)=>a-b).map(v=>{s=s||v;if(e&&v>e+1){o+=`${s<e?s+'-'+e:s} `;s=v}e=v});return o+(s<e?s+'-'+e:e)}
```
## ES5 variant, ~~198~~ ~~184~~ ~~183~~ 174 bytes
```
f=function(n){s=e=o='';n.split(' ').map(Number).sort(function(a,b){return a-b}).map(function(v){s=s||v;if(e&&v>e+1){o+=(s<e?s+'-'+e:s)+' ';s=v}e=v});return o+(s<e?s+'-'+e:e)}
```
```
f = function (n) {
    s = e = 0, o = '';
    n.split(' ').map(Number).sort(function (a, b) {
        return a - b
    }).map(function (v) {
        s = s || v;
        if (e && v > e + 1) {
            o += (s < e ? s + '-' + e : s) + ' ';
            s = v
        }
        e = v
    });
    return o + (s < e ? s + '-' + e : e)
}
// Demonstration
document.body.innerHTML = f('1 2');
document.body.innerHTML += '<p>' + f('9 13 3 11 8 4 10 15 6');
document.body.innerHTML += '<p>' + f('11 10 6 9 13 8 3 4 15');
document.body.innerHTML += '<p>' + f('5 8 3 2 6 4 7 1');
document.body.innerHTML += '<p>' + f('5 3 7 1 9');
```
[Answer]
# Ruby, 70 bytes
Problems like these tend to make me check the Ruby API for suitable methods, and today I discovered a new one: [`Array#slice_when`](http://ruby-doc.org/core-2.2.2/Enumerable.html#method-i-slice_when), newly introduced in Ruby v2.2 and seemingly intended for this exact situation :)
```
f=->a{puts a.sort.slice_when{|i,j|j-i>1}.map{|x|x.minmax.uniq*?-}*' '}
```
After sorting and appropriately slicing the array, it takes each sub-array and creates a string out of the highest and lowest element, and then joins this whole array into a string. 
Example:
`f.call [9,13,3,11,8,4,10,15,6]` prints `3-4 6 8-11 13 15`
[Answer]
# SWI-Prolog, ~~165~~ ~~162~~ 159 bytes
```
b(Z,C,[D|E]):-Z=[A|B],(A=:=D+1,(B=[],put(45),print(A);b(B,C,[A,D|E]));(E=[],tab(1),print(A);writef('-%t %t',[D,A])),b(B,A,[A]));!.
a(A):-sort(A,B),b(B,_,[-1]).
```
Pretty bad but then again Prolog is a terrible golfing language
Example: `a([9,13,3,11,8,4,10,15,6]).` outputs `3-4 6 8-11 13 15`
[Answer]
# Ruby, ~~86~~ 84 bytes
```
s=->*a{puts a.sort.slice_when{|i,j|i+1!=j}.map{|e|e.size<2?e:[e[0],e[-1]]*"-"}*" "}
# demo
s[9, 13, 3, 11, 8, 4, 10, 15, 6]
# => 3-4 6 8-11 13 15
```
This is a slightly golfed version from an example in the docs for [slice\_when](http://ruby-doc.org/core-2.2.2/Enumerable.html#method-i-slice_when).
[Answer]
# CJam, 35 bytes
```
l~${__0=f-ee::=0+0#/((oW>Wf*S+oe_}h
```
Try it online in the [CJam interpreter](http://cjam.aditsu.net/#code=l~%24%7B__0%3Df-ee%3A%3A%3D0%2B0%23%2F((oW%3EWf*S%2Boe_%7Dh&input=%5B9%2013%203%2011%208%204%2010%2015%206%5D).
### How it works
```
l~$     e# Read a line from STDIN, evaluate it and sort the result.
{       e# Do:
  _     e#   Push a copy of the array.
  _0=f- e#   Subtract the first element from all array elements.
  ee    e#   Enumerate the differences: [0 1 4] -> [[0 0] [1 1] [2 4]]
  ::=   e#   Vectorized quality: [i j] -> (i == j)
  0+    e#   Append a zero.
  0#    e#   Push the first index of 0.
  /     e#   Split the array into chunks of that size.
  (     e#   Shift out the first chunk.
  (o    e#   Print its first element.
  W>    e#   Discard all remaining elements (if any) except the last.
  Wf*   e#   Multiply all elements of the remainder by -1.
  S+o   e#   Append a space and print.
  e_    e#   Flatten the rest of the array.
}h      e# Repeat while the array is non-empty.
```
[Answer]
# CJam, ~~38~~ 33 bytes
New version, using ideas and code fragments suggested by @Dennis:
```
l~$_,,.-e`{~T+\_T+:T;(_2$+W*Q?S}/
```
[Try it online](http://cjam.aditsu.net/#code=l~%24_%2C%2C.-e%60%7B~T%2B%5C_T%2B%3AT%3B(_2%24%2BW*Q%3FS%7D%2F&input=%5B9%2013%203%2011%208%204%2010%2015%5D)
The input format is a CJam array in square brackets.
The basic idea here is that I subtract a monotonic sequence from the sorted input sequence first:
```
3  4  8  9 10 11 13 15
0  1  2  3  4  5  6  7  (-)
----------------------
3  3  6  6  6  6  7  8
```
In this difference, values that are part of the same interval have the same value. Applying the CJam RLE operator to this difference directly enumerates the intervals.
The subtracted sequential values need to be added back during output. I'm not entirely happy with how that's done in my code. I suspect that I could save a few bytes with a more elegant way of handing that.
For generating the output of the intervals, this uses Dennis' idea of generating a negative number for the end value, which takes care of producing a `-`, and also simplifies the logic because only one value needs to be added/omitted depending on the interval size.
Explanation:
```
l~    Get input.
$     Sort it.
_,,   Create monotonic sequence of same length.
.-    Calculate vector difference between the two.
e`    Calculate RLE of difference vector.
{     Loop over entries in RLE.
  ~     Unpack the RLE entry, now have length/value on stack.
  T+    Add position to get original value for start of interval.
  \     Bring length of interval to top of stack.
  _T+:T;  Add length of interval to variable T, which tracks position.
  (     Decrement interval length.
  _     Copy it, we need it once for calculating end value, once for ternary if condition.
  2$    Copy interval start value to top...
  +     ... and add interval length - 1 to get end value.
  W*    Negate end value.
  Q?    Output end value if interval length was > 1, empty string otherwise.
  S     Add a space.
}%    End loop.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 16 bytes
```
ṢI’kƊ.ịⱮUQ€j€”-K
```
[Try it online!](https://tio.run/##y0rNyan8///hzkWejxpmZh/r0nu4u/vRxnWhgY@a1mQB8aOGubre/w@3A5mR//9HW@ooGBrrKACRoaGOgoWOggmQZQDEpjoKZrE6CtEgYZCAmY4CVK0FWDlInSlIgSlMxAisCChuDpSCyhhDeEC9sQA "Jelly – Try It Online")
## How it works
```
ṢI’kƊ.ịⱮUQ€j€”-K - Main link. Takes an array L on the left
Ṣ                - Sort L; Call this A
    Ɗ            - Monad. Link the previous three links together f(A):
 I               -   Forward differences
  ’              -   Decrement
   k             -   Partition
                   This groups consecutive runs
                   Take A = [3, 4, 6, 8, 9, 10, 11, 13, 15]
                   I: [1, 2, 2, 1, 1, 1, 2, 2]
                   ’: [0, 1, 1, 0, 0, 0, 1, 1]
                   k then partitions A at truthy elements in I’
                   [[3, 4], [6], [8, 9, 10, 11], [13], [15]
       Ɱ         - For each:
     .ị          -   Take the 0.5th element
                   For non-integer left argument, x, Jelly's ị atom takes
                   floor(x) and ceil(x) and returns [floor(x)ịy, ceil(x)ịy]
                   As Jelly's indexing is 1-based and modular:
                     0ị returns the last element
                     1ị returns the first element
                     .ị returns [0ị, 1ị]
        U        - Reverse each
         Q€      - Deduplicate each
           j€”-  - Join each with "-"
               K - Join by spaces
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~21~~ 20 bytes
```
;moJ'-?I§e←→ε†sġ·=←O
```
[Try it online!](https://tio.run/##yygtzv7/3zo330td197z0PLUR20THrVNOrf1UcOC4iMLD223BQr4////P9pSx9BYx1jH0FDHQsdEx9BAx9BUxywWAA "Husk – Try It Online")
A bit lacking in the strings department, but does well.
No matter what I tried, it somehow stubbornly stayed at 21.
-1 byte from Dominic Van Essen.
## Explanation
```
;moJ'-?I§e←→ε†sġ§=…eO
                    O order the list
               ġ      group the list by the following:
                   e  elements paired
                §=    equal
                  …   the range between them?
             †s       convert all the numbers to strings
 mo                   map each group to the following:
      ?     ε         if it's a single element:
       I              leave as is
        §e←→          otherwise, move the first and last elements to a list
   J'-                join with '-'
;                     nest to display in one line
```
[Answer]
## CoffeeScript, ~~178~~ 161 bytes
Just like my JavaScript answer. I need to figure out if using comprehensions will result in shorter code.
```
f=(n)->s=e=o='';n.split(' ').map(Number).sort((a,b)->a-b).map((v)->s=s||v;(o+=s+(if s<e then'-'+e else'')+' ';s=v)if(e&&v>e+1);e=v);o+(if s<e then s+'-'else'')+e
```
---
Original:
```
f=(n)->o='';s=e=0;n.split(' ').map(Number).sort((a,b)->a-b).forEach((v,i)->if!i then s=v else(o+=s+(if s<e then'-'+e else'')+' ';s=v)if(v!=e+1);e=v);o+(if s<e then s+'-'else'')+e
```
[Answer]
# Python 2, ~~126~~ ~~122~~ 121 Bytes
I know this can get shorter, just don't know where.. Requires input in form `[#, #, #, #, ..., #]`.
```
l=sorted(input());s=`l[0]`;c=0;x=1
while x<len(l):y,z=l[x],l[x-1];s+=(('-'+`z`)*c+' '+`y`)*(y-z>1);c=(y-z<2);x+=1
print s
```
[Answer]
# Java, 191 bytes
```
void f(int[]a){java.util.Arrays.sort(a);for(int b=a.length,c=b-1,i=0,j=a[0],l=j;++i<b;){if(a[i]!=++j||i==c){System.out.print((l+1==j?l+(i==c?" "+a[c]:""):l+"-"+(i==c?j:j-1))+" ");l=j=a[i];}}}
```
Checks for ranges and prints them accordingly. Unfortunately I had to make a special case for the last element in the array since the program would terminate without printing the last number or range.
[Answer]
# Java, ~~171~~ 162 bytes
```
String s(int[] n){Arrays.sort(n);int p=0,b=0;String r="",d="";for(int c:n){if(c==++p)b=1;else{if(b==1){r+="-"+--p+d+c;p=c;b=0;}else{r+=d+c;p=c;}d=" ";}}return r;}
```
Takes input as an int array, returns output as a space-separated String list
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-S`](https://codegolf.meta.stackexchange.com/a/14339/), ~~17~~ 16 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
ÍòÈnYÉÃ®é ¯2 Ôq-
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVM&code=zfLIblnJw67pIK8yINRxLQ&input=WzkgMTMgMyAxMSA4IDQgMTAgMTUgNl0)
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `S`, 120 [bitsv1](https://github.com/Vyxal/Vyncode/blob/main/README.md), 15 bytes
```
s:¯Ǐ⁽ṅḋ•ꜝƛ₍htU\-j
```
[Try it Online!](https://vyxal.pythonanywhere.com/?v=1#WyJTfj0iLCIiLCJzOsKvx4/igb3huYXhuIvigKLqnJ3Gm+KCjWh0VVxcLWoiLCIiLCIhKC4qKSAtPiBcIiguKilcIlxuWzYsIDEsIDIsIDQsIDMsIDUsIDgsIDcsIDEwXSAtPiBcIjEtOCAxMFwiXG5bMTEsIDEwLCA2LCA5LCAxMywgOCwgMywgNCwgMTVdIC0+IFwiMy00IDYgOC0xMSAxMyAxNVwiXG5bOSwgMTMsIDMsIDExLCA4LCA0LCAxMCwgMTUsIDZdIC0+IFwiMy00IDYgOC0xMSAxMyAxNVwiXG5bNSwgOCwgMywgMiwgNiwgNCwgNywgMV0gLT4gXCIxLThcIlxuWzUsIDMsIDcsIDEsIDldIC0+IFwiMSAzIDUgNyA5XCIiXQ==)
## Explained
```
s:¯Ǐ⁽ṅḋ•ꜝƛ₍htU\-j
s:                 # Push two copies of the input list but sorted
  ¯Ǐ               # Forward differences of items with the first item appended - this is for making sure the length of the deltas matches the length of the input
    ⁽ṅḋ            # Grouped by whether they are <= 1
       •           # Reshape the sorted input to that shape
        ꜝ          # and remove the 0
                   # the top of the stack will be a list of lists of numbers that are ranges
         ƛ         # to each range
          ₍htU     #   either get the first and last item or just the first
              \-j  #   join on "-"
# S flag joins the result on spaces
```
[Answer]
# [Haskell](https://www.haskell.org/), 187 bytes
```
import Data.List
g p[x]=[[x]]
g p(a:b:c)|let(d:e)=g p(b:c)=if p a b then(a:d):e else[a]:(d:e)
p[x]=show x++" "
p x=show(head x)++"-"++show(last x)++" "
f=concatMap p.g(((-1==).).(-)).sort
```
[Try it online!](https://tio.run/##JY7LCoMwFET3fsVFukgwhoY@aAPZddl@gbi41WhCo4YmUBf9dxvtZmAOh2EMhpd2blns4Kd3hBtG5HcbYtaDr@ZaVSnqtRCUT9nQr9ORtFJTtbKVKNuBB4QnRKPHpLVUatAu6AprubnZNhXM9IG5KHLIMw/z1onR2MJMEy3zotiQwxD/KImdaqaxwfhAD573hJBSKEU55aSklId0ehnQjsq/7Rh33a66MnFgByYEu7AjE3smTuxcLz8 "Haskell – Try It Online")
[Answer]
# [Scala](https://www.scala-lang.org/), 204 bytes
Modified from [@steenslag's Ruby answer](https://codegolf.stackexchange.com/a/52328/110802).
---
Golfed version. [Try it online!](https://tio.run/##PU9NT8QgEL3vr5g0HhiLuF0/oigmHj142nja9ICU3aC0W4GYaMNvr1OMkpkwvOG9mReN9no@vr5Zk@BZuwGmubN7iEzLpyGdotzaj902BTccWjV9ag9RaRGPIdnujpUmJX1t2@XFIlsj4rmMInrXEYttECdm@Ijqwe3ZSP26UWpkDaIRbnBJ1swIryMVBbU@WjCyXuQKkEWvRxYKP4jovu39BoPo33/3gkKI1ckUSDyf0V3kcoV5Bljs9OSM6XCIEh5D0F9/jlDCC20ACqYV0BkJTX4gF7ccmgsOFE3D4YbDJVVryisO1/g/m1VQIRI3r/L8Aw)
```
def s(a:Int*):Seq[String]={val s=a.sorted;(Seq[Seq[Int]](Seq(s(0)))/:s.sliding(2)){(c,p)=>if(p(0)+1==p(1))c.init:+(c.last:+p(1))else c:+Seq(p(1))}.map(r=>if(r.size<2)r.mkString else s"${r(0)}-${r.last}")}
```
Ungolfed version. [Try it online!](https://tio.run/##XVHRSsMwFH3fVxyKD4mrYZ1TtFjBR2G@KD6NPVzbdEaztiRB0NFvr2myDTZIQs49595zL9eWpGkY2o8vWTq8kGqwmwCVrGEZ5Xhu3CXP8d4ohyJQwA9p2NY4WfkQifg9Y16p2Ujr@QiF1apSzYbNuahbXS1l7dhSWbcKj3dZrwNme/2npIpzjh0YlWWKjpThKB6DDaBqsDHEZhxTZCiKoGCZT/F6ocZ@82lIFppsAAfFvobUVo7ikQrWJ3x/HMicjhInE1vqWGB8U6GdAIRVfxIPmPOYJrbfb874waObTS52MT7O118d0Nhhn0TjzsudbmI9eyzAEiShtX6y38/WL4uR2dgcT8bQ7yoK1@frsuw@RXadwp8sS3GXYuF/M39vUtzGmv1kGP4B)
```
object Main {
  def s(a: Int*): Unit = {
    val sorted = a.sorted
    val sortedRanges = sorted.sliding(2).foldLeft(List[List[Int]](List(sorted.head))) { (acc, pair) =>
      if (pair(0) + 1 == pair(1)) acc.init :+ (acc.last :+ pair(1))
      else acc :+ List(pair(1))
    }
    val ranges = sortedRanges.map(range => if (range.size < 2) range.mkString else s"${range.head}-${range.last}")
    println(ranges.mkString(" "))
  }
  def main(args: Array[String]): Unit = {
    s(9, 13, 3, 11, 8, 4, 10, 15, 6)
  }
}
```
[Answer]
# [Ly](https://github.com/LyricLy/Ly), 35 bytes
```
a0r[r0Ir::u[ps`=]pl~!['-olu!]p' o];
```
[Try it online!](https://tio.run/##y6n8/z/RoCi6yMCzyMqqNLqgOME2tiCnTjFaXTc/p1QxtkBdIT/W@v9/Q0MuQwMuMy5LLkNjLgsuYy4TLkNTAA "Ly – Try It Online")
This is pretty brute force. It sorts the numbers then iterates until all the numbers have been consumed. After printing the "current" number on the stack, an inner loop consumes all the entries after that which are consequitive. Then it either appends a `-#` string or not before printing a space and iterating again.
```
# prep work... created sorted list of numbers with trailing "0"
a0r
a    - read numbers from STDIN, sort stack
 0   - push a "0"
  r  - reverse stack
# loop over all numbers, then exit
[                             ];
[                             ]   - iterate while top of stack not "0"
                               ;  - exit to avoid dumping junk on stack
# remember the current number for comparison later
r0Ir
r     - reverse stack
 0I   - copy bottom of stack entry to the top
   r  - reverse the stack
# print the current number, loop/consume all sequential nums on stack
::u[ps`=]p
::          - duplicate top of stack twice
  u         - print current number
   [    ]p  - loop until top of stack is "0"
    p       - delete previous number
     s      - save the current number
      `=    - increment current number, compare to next num on stack
# print "-X"  if needed to cover delete numbers, then print space    
l~!['-olu!]p' o
l                - load saved num (last number deleted)
 ~               - search stack for that number
  !              - negate result
   [      ]p     - "then" code block
    '-o          - print "-"
       lu        - load last num in sequence (last deleted)
         !       - flip top of stack to "0" to exit loop
            ' o  - print " "
```
[Answer]
# [Perl 5](https://www.perl.org/) `-pa`, 95 bytes
```
$_=join$",sort{$a-$b}@F;$b=$_,s/(\d+)\K (?=(\d+))/$2-$1-1?$":"-"/ge while$_ ne$b;s/(-\d+)+/$1/g
```
[Try it online!](https://tio.run/##HcvNCoJAFIbhW/kYzkLR08yxLErEVm2iOxBEaTBDHFGhRXTrTT@7d/G8o5361Huq8rvrBlLx7KblSTVT8zqeMmpyquJZB@U1CsszgiL/Z6gpYRKWgtRBsdKtxePW9ZYqDJaa7LvwD0aaRLfeb5Biiz0EO4iBCCRBYrB@u3Hp3DB7vqQrI8ZzPX4A "Perl 5 – Try It Online")
]