I was talking on #perl one
day, when someone joined and asked:
foldr (\(a,b) -> (a:).(b:)) [] $ uncurry zip \
("Hv o edyu IPtdy","aeyura orSC oa?")
<- What would this be in Perl? (It outputs
"Have you read your SICP today?")
In case you don't know your Haskell, what it does is interleave the two
strings and have 1 letter from here and the other one from here. In Perl,
strings are not lists or sequences, so we understood we'd need to split
them first to do something like that. Furthermore, we recalled the
"zip" function from
List-MoreUtils
that may prove to be useful.
Eventually, someone came with this Perl 5 solution:
perl -MList::MoreUtils=zip -e 'my @former = split //, "Hv o edyu IPtdy";
my @latter = split //, "aeyura orSC oa?";
print join "", zip @former, @latter'
Assigning to array variables was necessary because zip has a prototype. Then
I figured out that we can avoid that by over-riding the protoype using
"&zip", and proposed this solution:
perl -MList::MoreUtils=zip -e 'print join "", &zip(map { [split//, $_] }
"Hv o edyu IPtdy","aeyura orSC oa?")'
Which was much shorter and clearer but used "&zip". What happens is that
we pass it the arrays directly as references. I was able to golf it somewhat
into:
perl -MList::MoreUtils=zip -E 'say&zip(map[split//],"Hv o edyu IPtdy",
"aeyura orSC oa?")'
So we got what appears to be a valid use of "&func" to over-ride a pesky
prototype. Now, Perl 6 hackers are invited to contribute a Perl 6 solution.
|