#!/usr/local/bin/runhaskell import Data.List (sort) a = [45,30,28,27,20,1,2,3,4,29] sa = sort a span :: (Num a, Ord a) => [a]-> String span [] = "" span xs = find_span (sx,sx) sxs "" where (sx:sxs) = sort xs -- ensure list is sorted show_pair (low,high) | low == high = show low show_pair (low,high) | otherwise = (show low) ++ "-" ++ (show high) find_span state [] acc = (acc ++ show_pair state) find_span state@(low, high) (x:xs) acc | x == high+1 = find_span (low,x) xs acc find_span state@(low, high) (x:xs) acc | x > high = find_span (x,x) xs (acc ++ (show_pair state) ++ ",")