[Haskell-beginners] Writing a polymorphic function in Haskell
aditya siram
aditya.siram at gmail.com
Fri Sep 22 20:14:29 UTC 2017
Not tested but maybe adding an Eq instance to your datatype would help? So
try changing 'deriving (Show)' to 'deriving (Show, Eq)'.
On Fri, Sep 22, 2017 at 2:55 PM, Matt Williams <matt.williams45.mw at gmail.com
> wrote:
> Apologies - sent too soon - code edited below
>
> Dear All,
>>
>> I am having problems writing a polymorphic function.
>>
>> I have a type that has two constructors:
>>
>> data Arg = IndArg {evi::Evi, t1::Treatment, t2::Treatment, out::Outcome,
>> dir::Direction}
>> | MetaArg {target::EviType}
>> deriving (Show)
>>
>> I have a function checks for conflict:
>>
>> checkConflict :: Arg -> Arg -> Bool
>> checkConflict a b = if t1 a == t1 b && t2 a == t2 b && dir a /= dir b
>> then True
>> else False
>>
>> However, I can't make this work with both types of Argument - if I pass
>> it MetaArg, it raises an error.
>>
>> In another language, I would write something like:
>>
>> checkConflict(ArgA,ArgB):
>>
> if type(ArgA) == IndArg:
> <do this>
> elif type(ArgA) == MetaArg:
> <do other>
>
> Any thoughts would be welcomed.
>
> BW,
> Matt
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170922/5e69bd53/attachment.html>
More information about the Beginners
mailing list