[Haskell-cafe] trivial function application question

tphyahoo tphyahoo at gmail.com
Fri Jan 5 07:22:56 EST 2007

yes, I admit it's a perler attitude.

I still want to know how to do "s///" in haskell. My guess is that for a
"real" regex substitution scenario, you would use parsec. Is that more or
less correct, or are there other (easier?) approaches. It would of course be
great to be able to transfer as much of my PCRE domain experience as
possible to haskell.

eg in perl: 

use strict;
use warnings;

# phone numbers
my $strings  = [
                "123 456 7890",
                "123  456 7890",
                "789 234 1"

for my $string ( @$strings ) {
  my $formatted = tel_num($string);
  print "$formatted\n" if $formatted;

sub tel_num {
  my $string = shift or die "no string";
  return unless $string =~ /(\d\d\d)\s+(\d\d\d)\s+(\d\d\d\d)/;
  return "$1-$2-$3"

outputs (not just identified match, prettied up as well): 


so, best done with parsec, or some other way?

Jules Bean wrote:
> tphyahoo wrote:
>> So the core question (speaking as a perler) is how do you write
>>   my $s= 'abcdefg';
>>   $s =~ s/a/z/g;
>>   $s =~ s/b/y/g;
>>   print "$s\n";
>>  in haskell? There are various haskell regex libraries out there,
> But that's such a perler attitude. When all you have is a regex, 
> everything looks like a s///!
> This really doesn't look like much of a regex question to me. A more 
> haskelly answer might be as simple as:
> m 'a' = 'z'
> m 'b' = 'y'
> m  x  = x
> test1 = map m "abcdefg"
> ...which is general in the sense that 'm' can be an arbitrary function 
> from Char -> Char, and avoids the 'overlapping replace' behaviour 
> alluded to elsewhere in this thread, but is limited if you wanted to do 
> string-based replacement.
> To do string-based replacement you do have to think careful about what 
> semantics you're expecting though (w.r.t. overlapping matches, repeated 
> matches, priority of conflicting matches).
> Jules
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

View this message in context: http://www.nabble.com/trivial-function-application-question-tf2922232.html#a8177140
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list