Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

by BrowserUk (Pope)
on Aug 12, 2007 at 14:34 UTC ( #632061=note: print w/ replies, xml ) Need Help??


in reply to Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

Given that haskell's group is a library routine defined (one flavour) as:

group = groupBy (==) groupBy _ []= [] groupBy eq (x:xs)= (x:ys) : groupBy eq zs where (ys,zs) = span (eq x) xs span p [] = ([],[]) span p xs@(x:xs') | p x = (x:ys, zs) | otherwise = ([],xs) where (ys,zs) = span p xs'

it would be equivalent to hide the complexity of a perl equivalent in a function, and it might be defined something like this:

sub group { map{ pack 'C*', @$_ } @{ +reduce { defined $a->[0] && $a->[-1][0] && $a->[-1][0] == $b ? push @{ $a->[-1] }, $b : push @{ $a }, [ $b ]; $a; } [], unpack 'C*', shift } }

Giving a roughly equivalent one line usage:

print for group 'ZBBBCCZZ'; Z BBB CC ZZ

Of course, the equivalence is only rough because Haskell's syntactic sugar of allowing a list of chars to look like a string. The above won't work for an arbitrary list.

I remember suggesting that it would be nice if Perl 6 would allow a scalar to be treated syntactically as a list of chars: $scalar[3] = 'x'; etc. It would allow a single routine to be polymorphic to arbitrary lists and scalars and simplify the above, but I guess it wouldn't work for many other cases.

BTW, does anyone know of a CPAN module that can solve this problem directly?

I just did a codesearch for lang:haskell 'group', and without promising to have exhaustively searched every link, I couldn't find a single use of group in any of the first 5 or so pages of hits.

Loads of definitions, mostly the same but some variations in different libraries, but no uses.

I guess that's why you won't find it on cpan. Other than for essentially academic uses like this, there are very few uses for it.

Of course, now someone will turn up a 300 lines haskell module with 250 uses of group, but on the basis of what I found, it's not exactly an essential function.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://632061]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-11-28 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls