[Haskell-cafe] Substituting values
Jason Dusek
jason.dusek at gmail.com
Sat Dec 22 16:12:52 CET 2012
2012/12/21 Radical <radical at google.com>:
> 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
More information about the Haskell-Cafe
mailing list