2012/12/21 Radical <radi...@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

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to