Making my own type belong to Data.Ord

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Making my own type belong to Data.Ord

Patrik Iselind
Hi,

I'm trying to follow the "Real World Haskell" book online and have
reached exercise 12 in chapter three. See the bottom of
http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 
for reference.

It's the very last exercise on the page concerning constructing a Convex
Hull. I have no problem implementing a Graham Scan in non-functional
languages as i have far more experience with those. But the book hasn't
yet covered how i make my own data belong to for example the Data.Ord
class. The question then for me is how am i supposed to sort my coordinates?

Type classes are not covered for another three chapters in the book.
Given this it seems to me that i should be able to solve this without
using Data.Ord, i just cannot see how. Any pointers would be highly
appreciated.

--
Patrik Iselind

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Making my own type belong to Data.Ord

Francesco Ariis
On Sat, Jan 20, 2018 at 09:20:33PM +0100, Patrik Iselind wrote:
> It's the very last exercise on the page concerning constructing a Convex
> Hull. I have no problem implementing a Graham Scan in non-functional
> languages as i have far more experience with those. But the book hasn't yet
> covered how i make my own data belong to for example the Data.Ord class. The
> question then for me is how am i supposed to sort my coordinates?
>
> Type classes are not covered for another three chapters in the book. Given
> this it seems to me that i should be able to solve this without using
> Data.Ord, i just cannot see how. Any pointers would be highly appreciated.

Hello Patrik,
    as you suggested, implementing an instance of Data.Ord would be ok; if
you don't want that, just write a

    myTypeCompare :: SomeData -> SomeData -> Ordering

function to use with `sortBy` from `Data.List`.
Does that make sense?
-F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Making my own type belong to Data.Ord

Patrik Iselind

Den 2018-01-20 kl. 21:27, skrev Francesco Ariis:

> On Sat, Jan 20, 2018 at 09:20:33PM +0100, Patrik Iselind wrote:
>> It's the very last exercise on the page concerning constructing a Convex
>> Hull. I have no problem implementing a Graham Scan in non-functional
>> languages as i have far more experience with those. But the book hasn't yet
>> covered how i make my own data belong to for example the Data.Ord class. The
>> question then for me is how am i supposed to sort my coordinates?
>>
>> Type classes are not covered for another three chapters in the book. Given
>> this it seems to me that i should be able to solve this without using
>> Data.Ord, i just cannot see how. Any pointers would be highly appreciated.
> Hello Patrik,
>      as you suggested, implementing an instance of Data.Ord would be ok; if
> you don't want that
I wouldn't mind writing it, at all. It's probably the way i would do it
if i knew more Haskell. My problem is just that the book i follow hasn't
covered that topic yet, so even if i could google how to do it i guess i
shouldn't until the book has covered the topic.

As i understand it this would be the 'preferred' way in the real world,
is this correctly understood?
> , just write a
>
>      myTypeCompare :: SomeData -> SomeData -> Ordering
>
> function to use with `sortBy` from `Data.List`.
> Does that make sense?
That would make sense, i seem to remember something along those lines
from previous topics covered in the book.

Thanks a lot Francesco, much appreciated!

// Patrik
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners