
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

This is reminiscent of the Either (exception) monad where Left values, the
exceptions, pass through unaltered, and Right values are transformable,
i.e. acted on by functions.
But I have no idea what you're trying to achieve in the bigger picture.
Help us help you by fleshing out your use case.
-- Kim-Ee
On Sat, Dec 22, 2012 at 12:46 AM, Radical
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, Dec 22, 2012 at 12:35 AM, Kim-Ee Yeoh
This is reminiscent of the Either (exception) monad where Left values, the exceptions, pass through unaltered, and Right values are transformable, i.e. acted on by functions.
Interesting. I hadn't thought of that parallel. But yes, I think it's a similar notion of: transform the value only in one (or more) cases. It's also similar to the Maybe monad in that I'm looking for something akin to fromMaybe, but without having to place the value in a Maybe.
But I have no idea what you're trying to achieve in the bigger picture. Help us help you by fleshing out your use case.
I'm not sure the bigger picture is helpful, but, for example, the last case was something like: if ch == '\n' then ' ' else ch I think I ended up rewriting it as something like: replace '\n' = ' ' replace ch = ch Which is straightforward enough, though it feels to me like there's too much syntax involved. Thanks, Alvaro

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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
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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Alvaro,
I think you need something wha Scala has - the ability to create a partial
function from a case expression. In Scala you could write
def update[A](f: PartialFunction[A,A])(v: A): A =
f.orElse({ case x => x } : PartialFunction[A,A]).apply(v);
and then use it like
update[Int]({ case Foo => Bar })
But AFAIK there is nothing like this in Haskell. Maybe separating 'of' from
'case' would be the way to extend Haskell with such a feature <
http://www.haskell.org/pipermail/haskell-cafe/2012-November/104884.html>
Best regards,
Petr
2012/12/21 Radical
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hey Petr,
On Sat, Dec 22, 2012 at 9:52 AM, Petr P
I think you need something wha Scala has - the ability to create a partial function from a case expression. In Scala you could write
def update[A](f: PartialFunction[A,A])(v: A): A = f.orElse({ case x => x } : PartialFunction[A,A]).apply(v);
and then use it like
update[Int]({ case Foo => Bar })
Thanks for the pointer. One distinction I remember from dabbling in Scala some years ago is that Scala seems to formalize partiality whereas, if I'm not mistaken, Haskell doesn't (though perhaps there are libraries that let you do that). That is, to me a partial function in Haskell is almost uniformly an error, whereas in Scala it's a somewhat common pattern.
But AFAIK there is nothing like this in Haskell. Maybe separating 'of' from 'case' would be the way to extend Haskell with such a feature < http://www.haskell.org/pipermail/haskell-cafe/2012-November/104884.html>
Does Haskell have a way to query at runtime whether a function is partial? (Not in the full sense, since it would be necessary to prove totality, but in the sense that a case expression is not exhaustive.) Otherwise, I'm not sure how you could use a partial lambda without reaching `undefined`. (I guess you could catch the exception.)
Thanks, Alvaro

2012/12/21 Radical
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?
Haskell doesn't offer a compact "ternary operator" or similar construct. In some cases, a local definition and pattern guards is appealing: {-# LANGUAGE PatternGuards #-} f value = ... value' ... where value' | Foo <- value = Bar | otherwise = value This does not really have the intuitive of appeal that a "pattern matching ternary operator" might, though: f value = ... (Foo <- value ? Bar : value) ... In Haskell, working with patterns generally is cleaner with multiple lines and full indentation. -- Jason Dusek pgp // solidsnack // C1EBC57DC55144F35460C8DF1FD4C6C1FED18A2B

Since recently, the notion of prisms from the lens library can achieve
that : to modify a value only in certain conditions but you have to
write the prism so it's not that convenient, though at least you'll
have an uniform API.
See http://hackage.haskell.org/packages/archive/lens/3.7.0.2/doc/html/Control-Le...
, especially the "nat" example.
--
Jedaï
On Fri, Dec 21, 2012 at 6:46 PM, Radical
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (6)
-
Chaddaï Fouché
-
David Thomas
-
Jason Dusek
-
Kim-Ee Yeoh
-
Petr P
-
Radical