in reply to Print series of numbers

Not strictly speaking a one-liner, but Set::IntSpan can produce this kind of output.

Update: And just for grins . . .

#!/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 hi +gh) find_span state [] acc = (acc ++ sho +w_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) ++ ",")

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Print series of numbers
by gmargo (Hermit) on Oct 20, 2009 at 16:07 UTC

    That is super sweet. It does boil down to essentially a one-liner.

    #!/usr/bin/perl use strict; use warnings; use Set::IntSpan; my @list = (1, 2, 3, 6, 9, 10, 13, 22, 20, 19, 15, 21); my $rl = Set::IntSpan->new( \@list )->run_list(); print "$rl\n";