3 ms·
In Ruby: def englishlist(array) array[0..-2].join(', ').to_s + (array.size > 1 ? ' and ' : '') + array.last.to_s end (Not claiming it's as clear to re
by Xichekolas 18y ago
In Ruby:
def englishlist(array)
array[0..-2].join(', ').to_s + (array.size > 1 ? ' and ' : '') + array.last.to_s
end
(Not claiming it's as clear to read, but I miss my old one-liner contests! ;)
You can implement it roughly as clearly (although not as efficiently) as such:
def englishlist(array)
case array.length
when 0; ""
when 1; array.first
when 2; array.join(' and ')
else; array.first + ', ' + englishlist(array[1..-1])
end
end
I do love me the Haskell so far, but I'm only into the basics still. Love pattern matching, was one of my favorite aspects of Erlang.