[Haskell-cafe] trivial function application question

Donald Bruce Stewart dons at cse.unsw.edu.au
Fri Jan 5 21:03:10 EST 2007

> 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";

Simple patterns like this you'd just use a 'map' of course:

    main = print (clean "abcdefg")

    clean = map (by . az)
      where by c = if c == 'b' then 'y' else c
            az c = if c == 'a' then 'z' else c

Running this:

    $ runhaskell A.hs

Now, using regexes instead we can get by with just the regex-compat lib,

    import Text.Regex

I usually flip the arguments to subRegex, since they're in the wrong
order for composition (anyone else noticed this?):

    sub   re y s = subRegex re s y
    regex        = mkRegex

Now , using proper regexes, we can write:

    main  = print (clean "abcdefg")

    clean = sub (regex "b") "y"
          . sub (regex "a") "z"

Running this:

    $ runhaskell A.hs

Similar results will be achieved with the other regex-* packages:


I think TRE might be preferred for high performance cases.

-- Don

More information about the Haskell-Cafe mailing list