[Haskell-cafe] Substituting values

Radical radical at google.com
Tue Dec 25 02:52:22 CET 2012


Yeah, I guess I was wondering whether something like it already existed.
Thinking about it some more, perhaps what I want is `tr` [1]. E.g.

  tr [Foo] [Bar]

Incorporating your suggestion would then yield a more general version like:

  trBy (== Foo) Bar



[1] http://en.wikipedia.org/wiki/Tr_(Unix)


On Sat, Dec 22, 2012 at 6:46 AM, David Thomas <davidleothomas at gmail.com>wrote:

> Seems like the function is easy to define:
>
> replaceIfEq a b c = if c == a then b else c
>
> Then the above can be written
>
>    replaceIfEq Foo Bar value
>
> Or the slightly more general (in exchange for slightly more verbosity at
> the call site)
>
> replaceIf p r a = if p a then r else a
>
>    replaceIf (== Foo) Bar value
>
>
>
>
> On Fri, Dec 21, 2012 at 6:46 PM, Radical <radical at google.com> wrote:
>
>> Sometimes I'll need something like:
>>
>>   if value == Foo then Bar else value
>>
>> Or some syntactic variation thereof:
>>
>>   case value of { Foo -> Bar; _ -> value }
>>
>> Is there a better/shorter way to do it? I'm surprised that it's more
>> complicated to substitute a value on its own than e.g. in a list, using
>> filter. Or perhaps I'm missing the right abstraction?
>>
>> Thanks,
>>
>> Alvaro
>>
>>
>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121224/731c561f/attachment.htm>


More information about the Haskell-Cafe mailing list