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. Defining an Instance of Ord (Matt Williams)
2. Re: Defining an Instance of Ord (Petr V?penka)
3. Re: Defining an Instance of Ord (Imants Cekusins)
4. Re: Defining an Instance of Ord (Brandon Allbery)
5. Re: Defining an Instance of Ord (Imants Cekusins)
----------------------------------------------------------------------
Message: 1
Date: Fri, 26 Jun 2015 14:55:46 +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] Defining an Instance of Ord
Message-ID:
<caftvgqb-okpm+o+sb1enyhpv-9q49mrbny4hzt4hvmtcd2k...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Dear All,
I am trying to produce a Map, where the (tricky) idea is that the key is a
pair, (t1, t2), and the key is considered identical under ordering. Thus:
(t1, t2) is the same as (t2, t1) but
(t1, t3) is not the same as (t1,t2).
This LOOKS like a equality definition. However, the Map key typeclass is
defined as Ord, which requires me to define compare:
instance Ord Edge where
(Edge s1 _) `compare` (Edge s2 _) = s1 `compare` s2
I am a bit stuck on how to use compare to define this type of eqlaity
- any pointers very gratefully received.
BW,
Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150626/97f7a8bd/attachment-0001.html>
------------------------------
Message: 2
Date: Fri, 26 Jun 2015 16:11:50 +0200
From: Petr V?penka <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an Instance of Ord
Message-ID:
<candmeufjbzqxkm11hr74c+wkefdugo4vbbq2rqmt-3tpnea...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
What about using new type and sort the pair before comparing?
Dne 26.6.2015 15:56 "Matt Williams" <[email protected]>
napsal(a):
> Dear All,
>
> I am trying to produce a Map, where the (tricky) idea is that the key is a
> pair, (t1, t2), and the key is considered identical under ordering. Thus:
>
> (t1, t2) is the same as (t2, t1) but
> (t1, t3) is not the same as (t1,t2).
>
> This LOOKS like a equality definition. However, the Map key typeclass is
> defined as Ord, which requires me to define compare:
>
> instance Ord Edge where
> (Edge s1 _) `compare` (Edge s2 _) = s1 `compare` s2
>
> I am a bit stuck on how to use compare to define this type of eqlaity - any
> pointers very gratefully received.
>
> 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/20150626/e144f9a3/attachment-0001.html>
------------------------------
Message: 3
Date: Fri, 26 Jun 2015 16:26:11 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an Instance of Ord
Message-ID:
<cap1qinzeqrdkt4p-_zz5yex8992+n+y+r_ox0jdld3gajjl...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
> What about using new type and sort the pair before comparing?
Matt needs to define Ord instance for (t1,t2) so he can use (t1,t2) in
Map in non-standard way.
he is not trying to sort or compare (t1,t2) just for the sake of it.
I think..
------------------------------
Message: 4
Date: Fri, 26 Jun 2015 10:29:34 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an Instance of Ord
Message-ID:
<cakfcl4woktm7j2sswghdsdukr4u0wnexjxm5wqjgw9tp7mo...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Fri, Jun 26, 2015 at 9:55 AM, Matt Williams <[email protected]
> wrote:
> I am trying to produce a Map, where the (tricky) idea is that the key is a
> pair, (t1, t2), and the key is considered identical under ordering. Thus:
>
> (t1, t2) is the same as (t2, t1) but
> (t1, t3) is not the same as (t1,t2).
>
> This LOOKS like a equality definition. However, the Map key typeclass is
> defined as Ord, which requires me to define compare:
>
You need more than Eq to get a collection which can be searched
efficiently. Map uses Ord; Hashmap (in unordered-containers) uses Hashable,
and might be more appropriate for this type. You will still have to deal
with the pair, however.
Ord (or Hashable) is only used internally for searching, so you can define
an instance which does not necessarily do anything semantically meaningful.
For example, one way to define a `compare` for this is to sort the values
in the pairs and then apply compare between them:
-- assumes s, t are themselves known by compiler to be Ord
instance Ord Edge where
(Edge s1 t1) `compare` (Edge s2 t2) = let arb s t = if s < t then
(s,t) else (t,s)
in arb s1 t1 `compare` arb
s2 t2
A similar trick could be used to get a Hashable instance.
This would end up being slow for large maps or many lookups. In that case,
you might consider a wrapper which applies the above "arb" operation to the
key on insert or lookup (the "normalized" key is stored in the map), rather
than having to compute it for every node traversed during lookup.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150626/947802ff/attachment-0001.html>
------------------------------
Message: 5
Date: Fri, 26 Jun 2015 17:04:18 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an Instance of Ord
Message-ID:
<cap1qinb1j7vrbvnv_g6kvjzf0tt4hhbnc01_01acak74-h+...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
> What about using new type and sort the pair before comparing?
> sort the values in the pairs and then apply compare between them
now I see what Petr meant.
Thank you
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 84, Issue 46
*****************************************