[Haskell-cafe] Writing guards shorthand
Jules Bean
jules at jellybean.co.uk
Thu Apr 19 09:06:18 EDT 2007
Joel Reymont wrote:
> Support I want to infer the type given an Op that looks like this
> (incomplete):
>
> data Op
> = Minus
> | Plus
> | Mul
> | LT
> | GT
>
> Is there a shorthand way of bunching Minus, Plus and Mul in a function
> guard since they all result in TyNum whereas the rest in TyBool?
data NumOrBool = JNum | JBool deriving (Eq)
numorbool Minus = JNum
numorbool Plus = JNum
numorbool Mul = JNum
numorbool LT = JBool
numorbool GT = JBool
f o | numorbool o == JNum = TyNum (...)
| numorbool o == JBool = TyBool (...)
...so you have to define one function rather verbosely (numorbool) but
once you've done it once you can use it in a guard to define others more
quickly.
You could use pattern guards instead of == and forget about deriving Eq
if you prefer.
Jules
More information about the Haskell-Cafe
mailing list