[Haskell-beginners] using the joker "_" on a constructor
Kim-Ee Yeoh
ky3 at atamo.com
Wed Sep 18 16:21:47 CEST 2013
On Wed, Sep 18, 2013 at 9:08 PM, TP <paratribulations at free.fr> wrote:
> f :: Foo -> Bool
> f (Foo n)
> | even n = True
> | odd n = False
> f (Bar n)
> | even n = True
> | odd n = False
>
(1) Since even is monomorphized to Int -> Bool, you can just write
f (Foo n) = even n
f (Bar n) = even n
(2) Since the sum subtypes are the same, you can write
data Foo = Foo {myN :: Int} | Bar {myN :: Int}
f :: Foo -> Bool
f x = even $ myN x
(3) As to your original question why the underscore _ can't be overloaded
for this, I dunno. It /may/ be possible. Seems rare in practice. You could
always apply the type isomorphism to get
data FooT = Foo | Bar
data Foo = Foo Int FooT
f (Foo n _) = even n
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130918/28138c88/attachment.htm>
More information about the Beginners
mailing list