[Haskell-beginners] using the joker "_" on a constructor

Christian Maeder Christian.Maeder at dfki.de
Wed Sep 18 16:37:20 CEST 2013


In order to avoid your duplicate code consider to define "Foo" as follows:

  data Foo = F FB Int
  data FB = Foo | Bar

  f (F _ n) = even n

and insert the constructor "F" after the final
"$" in:

 > print $ f $ Bar 1
 > print $ f $ Bar 2
 > print $ f $ Foo 1
 > print $ f $ Foo 2

HTH Christian

Am 18.09.2013 16:08, schrieb TP:
> Hi,
>
> I have a question about pattern matching.
> Consider the following code:
>
> ------------------
> data Foo = Bar Int
>           | Foo Int
>
> f :: Foo -> Bool
> f (Foo n)
>      | even n = True
>      | odd n = False
> f (Bar n)
>      | even n = True
>      | odd n = False
>
> main = do
>
> print $ f $ Bar 1
> print $ f $ Bar 2
> print $ f $ Foo 1
> print $ f $ Foo 2
> ------------------
>
> Why is it not possible to simply write for f:
>
> f v = case v of
>      _ n | even n -> True
>      _ n | odd n -> False
>
> or
>
> f (_ n)
>      | even n = True
>      | odd n = False
>
> (in both cases we get a parse error)?
>
> Thanks,
>
> TP
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>




More information about the Beginners mailing list