[Haskell-cafe] trivial function application question

Neil Mitchell ndmitchell at gmail.com
Thu Jan 4 16:47:18 EST 2007


Hi Brad,

> i have a small problem that will be certainly trivial for almost
> everyone reading this, i would appreciate a little help

If you have trivial problems, its often useful to ask on Haskell IRC
(http://www.haskell.org/haskellwiki/IRC_channel)

> from which my intent is that "a" be replaced by "Z", "b" by "Y" etc
>
> i am sure there is an elegant way to apply replace to s for all of
> these argument pairs without composing replace N times myself, but the
> solution escapes me.

In your example all "strings" are one letter long, is that how this
works? If so, then you can simplify the problem significantly to use
Char's, and use the following library functions:

First off, if you want to apply the same "transformation" to each item
of a list, namely to either replace it or leave it the same. This
calls out for map.

Secondly you want to do lookups in some sort of table. The lookup
function can be very handy here. The lookup function works on
associative lists, so you'd need to zip patterns and replace into an
associative list.

If you really want to operate on strings, rather than characters, then
you have to be more clever. Also replace called multiple times
probably won't be enough, consider replacing 1 with 2, 2 with 3. If
you just call replace multiple times, 1 may well end up at 3, when 2
is more likely to be the right answer.

Thanks

Neil


More information about the Haskell-Cafe mailing list