Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Writing a polymorphic function in Haskell (Matt Williams)
2. Re: Writing a polymorphic function in Haskell (Matt Williams)
3. Re: Writing a polymorphic function in Haskell (aditya siram)
4. Re: Writing a polymorphic function in Haskell (David McBride)
----------------------------------------------------------------------
Message: 1
Date: Fri, 22 Sep 2017 20:53:55 +0100
From: Matt Williams <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Writing a polymorphic function in Haskell
Message-ID:
<caftvgqz8ei80gfm5qtnjgnmkbk3lfhqsfa67gcefs4roo95...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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):
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20170922/f2c55e22/attachment-0001.html>
------------------------------
Message: 2
Date: Fri, 22 Sep 2017 20:55:43 +0100
From: Matt Williams <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Writing a polymorphic function in
Haskell
Message-ID:
<caftvgqzs4isjfnpz1febi1qjxozchsqwqpfutjopmecucuh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20170922/e57fae1d/attachment-0001.html>
------------------------------
Message: 3
Date: Fri, 22 Sep 2017 15:14:29 -0500
From: aditya siram <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Writing a polymorphic function in
Haskell
Message-ID:
<CAJrReyiVqJKM=bxka_zuojombvchzyqsdupxthdw0wvxtl3...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]
> 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
> [email protected]
> 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-0001.html>
------------------------------
Message: 4
Date: Fri, 22 Sep 2017 16:16:55 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Writing a polymorphic function in
Haskell
Message-ID:
<can+tr40gkzbxxxhfrmnojyqaxxf0__jfzveqjhl2ukymcwy...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"
It is a mistake to use record syntax with data types with multiple
constructors. It creates a partial function that dies when you try to
use it on a constructor that does not have that field. It is a flaw
in the language that many people would like to see removed because it
allows programs to compile that will fail at run time, possibly
unexpectedly.
Arg can have a constructor that does not have an evi. Also what
happens if you compare an IndArg to a MetaArg? Or a MetaArg to
another MetaArg?
You should remove all of these record names, and then write
checkConflict as follows (warning untested):
checkConflict :: Arg -> Arg -> Bool
checkConflict (IndArg _ at1 at2 _ adir) (IndArg _ bt1 bt2 _ bdir) =
at1 == bt1 && at2 == bt2 && adir /= bdir
-- checkConflict (IndArg ...) (MetaArg) = ???
-- checkConflict a@(MetaArg ...) b@(IndArg ...) = checkConflict b a
-- checkConflict (MetaArg _) (MetaArg _) = ???
checkConflict _ _ -> False -- perhaps a catchall?
However, you may keep the records as long as you are absolutely sure
you are using them only in places where you have the appropriate
constructor.
On Fri, Sep 22, 2017 at 3:53 PM, Matt Williams
<[email protected]> wrote:
> 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):
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 111, Issue 14
******************************************