There's more than one way to do things PerlMonks

### Re^6: 99 Problems in Perl6

by gaal (Parson)
 on Dec 15, 2006 at 22:54 UTC ( #590145=note: print w/replies, xml ) Need Help??

in reply to Re^5: 99 Problems in Perl6
in thread 99 Problems in Perl6

The "alternative solution" posted there doesn't suffer from the monomorphism restriction*, and thus does less to propagate the myth that one has to be a genius to write code in Haskell. It also works correctly with empty input :-)

```compress []       = []
compress [a]      = [a]           -- singleton list
compress (x:y:xs) = (if x == y then [] else [x]) ++ compress (y:xs)
*Something that, for the casual programmer, is quite technical but uninteresting, has a scary name, and a not-that-friendly error message.

Replies are listed 'Best First'.
Re^7: 99 Problems in Perl6
by TimToady (Parson) on Dec 15, 2006 at 23:31 UTC
And indeed, you can do that in Perl too:
```multi compress () { () }
multi compress (\$a) { item \$a }
multi compress (\$x, \$y, *@xs) { \$x xx (\$x !=== \$y), compress(\$y, |@xs)
+ }

my @x = <a a a a b c c a a d e e e e>;
say perl compress |@x;
Didn't we end up using when for guards? The problem with multis for this I seem to remember was that it was hard to spec their order if they're defined in separate compilation units.
We'll support matching against sigs using when if you really want ordered guards, but multis are defined to ignore ordering, so it doesn't matter where they're defined, in this file or in any other file. Only the type semantics of the signature matter. Overuse of ordered guards is actually kind of a semantic flaw in Haskell, I think. It's a way of sneaking in sequential dependencies without a monad.

Create A New User
Node Status?
node history
Node Type: note [id://590145]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-04-22 17:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (83 votes). Check out past polls.

Notices?