<?xml version="1.0" encoding="windows-1252"?>
<node id="632023" title="Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)" created="2007-08-12 03:35:13" updated="2007-08-11 23:35:13">
<type id="120">
perlmeditation</type>
<author id="176576">
eyepopslikeamosquito</author>
<data>
<field name="doctext">
&lt;P&gt;
Following the educational tradition of:
 &lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.rosettacode.org/"&gt;Rosetta code&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html"&gt;99 Lisp Problems&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://svn.pugscode.org/pugs/t/examples/99problems/"&gt;99 Problems in Perl 6&lt;/a&gt;
  &lt;li&gt;[id://589260]
 &lt;/ul&gt;
this meditation describes an arbitrary problem to be solved
in as many different languages as possible.
&lt;/P&gt;

&lt;readmore&gt;

&lt;P&gt;
I chose this particular problem purely by chance after being
surprised that [id://629259|solving it in Perl]
turned out to be more awkward than I'd expected.
Curious as to whether this awkwardness was specific to Perl, I then tried solving it in Ruby, Python and Haskell. Hence this node. :-)
&lt;/P&gt;

&lt;P&gt;&lt;B&gt;The Problem&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;
Given an input string, for example &lt;CODE&gt;"ZBBBCZZ"&lt;/CODE&gt;, produce a list,
for example, &lt;CODE&gt;["Z", "BBB", "C", "ZZ"]&lt;/CODE&gt;.
&lt;/P&gt;

&lt;P&gt;
That is, break the input string into pieces based on change of character.
&lt;/P&gt;

&lt;P&gt;
My preferred solutions
in Perl, Ruby, Python and Haskell follow. Improvements welcome.
&lt;/P&gt;

&lt;P&gt;&lt;B&gt;Perl&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;
My favourite solution from [id://629259] was given in the first response by [tye]:
&lt;CODE&gt;
my $s = "ZBBBCZZ";
my @x;
push @x, $1 while $s =~ /((.)\2*)/g;
&lt;/CODE&gt;
&lt;/P&gt;

&lt;P&gt;&lt;B&gt;Ruby&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;
This was discussed at length on the Ruby-Talk mailing list,
with many different solutions offered.
My favourite is similar to [tye]'s Perl solution:
&lt;CODE&gt;
s = "ZBBBCZZ"
x = []
s.scan(/((.)\2*)/){x.push [$~[0]]}
&lt;/CODE&gt;
&lt;/P&gt;

&lt;P&gt;&lt;B&gt;Python&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;
This problem was also discussed on Python-list.
My favourite uses Python's SML-inspired
&lt;a href="http://docs.python.org/lib/module-itertools.html"&gt;itertools library&lt;/a&gt;:
&lt;CODE&gt;
import itertools
s = "ZBBBCCZZ"
x = [''.join(g) for k, g in itertools.groupby(s)]
&lt;/CODE&gt;
&lt;/P&gt;

&lt;P&gt;&lt;B&gt;Haskell&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;
Browsing the Haskell
&lt;a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html"&gt;Data.List documentation&lt;/a&gt;,
I noticed that a Haskell solution seems trivial courtesy of Data.List's &lt;CODE&gt;group/groupBy&lt;/CODE&gt; function:
&lt;CODE&gt;
group "ZBBBCCZZ"
&lt;/CODE&gt;
&lt;/P&gt;

&lt;P&gt;&lt;B&gt;Discussion&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;
As you might expect, the only solution that I found truly satisfying was the Haskell one :-) ...
though this seems to be more of a library issue than a language one.
&lt;/P&gt;

&lt;P&gt;
BTW, does anyone know of a CPAN module that can solve this problem directly?
I took a quick look at:
&lt;ul&gt;
 &lt;li&gt; [cpan://List::Util]
 &lt;li&gt; [cpan://List::MoreUtils]
 &lt;li&gt; [cpan://Iterator]
&lt;/ul&gt;
but didn't notice any obvious, trivial way to do it with those libraries.
&lt;/P&gt;

&lt;P&gt;
I'm also curious to learn about plans for Perl 6 "functional-style"
libraries. I've been impressed by Haskell's libraries and hope
they will help inspire some of the new Perl 6 library.
&lt;/P&gt;

&lt;P&gt;
Anyway, I'd find it interesting and educational to compare solutions to this little problem
in a variety of other languages. So, please respond away in your favourite language!
&lt;/P&gt;

&lt;P&gt;&lt;B&gt;References&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.rosettacode.org/"&gt;Rosetta code&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html"&gt;99 Lisp Problems (my problem is similar to P09)&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://svn.pugscode.org/pugs/t/examples/99problems/"&gt;99 Problems in Perl 6&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://haskell.org/haskellwiki/99_Haskell_exercises"&gt;Ninety-Nine Haskell Problems&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://mail.python.org/pipermail/python-list/2007-July/451112.html"&gt;Discussion of this problem on Python-list&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://www.ruby-lang.org/en/community/mailing-lists/"&gt;Ruby-Talk mailing list&lt;/a&gt;
  &lt;li&gt;[id://629259]
  &lt;li&gt;[id://468106]
  &lt;li&gt;[id://512768]
  &lt;li&gt;[id://589260]
  &lt;li&gt;[id://771219]
  &lt;li&gt;&lt;a href="http://mjtsai.com/blog/2002/11/25/perl_vs_python_vs_ruby/"&gt;Perl v Python v Ruby blog&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://svn.openfoundry.org/pugs/docs/Perl6/Spec/Functions.pod"&gt;Perl 6 S29 (Functions)&lt;/a&gt;
  &lt;li&gt;[cpan://List::Util]
  &lt;li&gt;[cpan://List::MoreUtils]
  &lt;li&gt;[cpan://Iterator]
  &lt;li&gt;&lt;a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html"&gt;Haskell Data.List documentation&lt;/a&gt;
  &lt;li&gt;&lt;a href="http://docs.python.org/lib/module-itertools.html"&gt;Python itertools documentation&lt;/a&gt;
 &lt;/ul&gt;
&lt;/P&gt;

&lt;P&gt;
&lt;small&gt;2-sep-2007: Updated references with some more links based on the responses below.&lt;/small&gt;
&lt;/P&gt;

&lt;/readmore&gt;
</field>
</data>
</node>
