[Haskell-cafe] trivial function application question

Jules Bean jules at jellybean.co.uk
Fri Jan 5 06:55:03 EST 2007


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


More information about the Haskell-Cafe mailing list