🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.functional
2 messages
2 total messages Started by James Tue, 02 Mar 1999 00:00
Haskell Operator Overloading. (Very) trivial newbie question.
#1134
Author: James
Date: Tue, 02 Mar 1999 00:00
21 lines
1490 bytes
Hello,

The following is not a homework question, just something that has been
testing my patience. I have an algorithm that sorts a list of pairs. Say one
instance of the input is of type,

:: ((a,a),(a,a))

I find that if I use the derived ordering it sorts, but the result is not
what I want. I want to introduce an ordering based on a function I already
have. For example a metric on the above input with type,

:: Floating a => ((a,a),(a,a)) -> a

My understanding is that I should be able to achieve this by operator
overloading. I have looked at both the gentle intro and the Hugs prelude but
have just not had much success expressing it precisely in Haskell.

An example from any of you would be greatly appreciated.

  Thanks in advance
Re: Haskell Operator Overloading. (Very) trivial newbie question.
#1135
Author: Adam P. Jenkins
Date: Tue, 02 Mar 1999 00:00
46 lines
2660 bytes
"James" <reply@newsgroup.com> writes:
> Hello,
> 
> The following is not a homework question, just something that has been
> testing my patience. I have an algorithm that sorts a list of pairs. Say one
> instance of the input is of type,
> 
> :: ((a,a),(a,a))
> 
> I find that if I use the derived ordering it sorts, but the result is not
> what I want. I want to introduce an ordering based on a function I already
> have. For example a metric on the above input with type,
> 
> :: Floating a => ((a,a),(a,a)) -> a
> 
> My understanding is that I should be able to achieve this by operator
> overloading. I have looked at both the gentle intro and the Hugs prelude but
> have just not had much success expressing it precisely in Haskell.
> 
> An example from any of you would be greatly appreciated.

First of all, overloading the comparison operators for your type is
only appropriate if you want to change the ordering for your type
everywhere in your program.  If you just want to sort differently,
then use the sortBy function instead of sort.  sortBy takes a
comparison function as an argument, so you can just pass your existing
comparison function.

If you do want to overload, you need to create a new type first, using
the data or newtype keyword.  You can't just overload type 
	Floating a => ((a,a), (a,a))
because that's an existing type.  You'd need to define something like

newtype Floating a => MyType a = Pair ((a,a), (a,a))

Now you can create instances of Eq and Ord for (MyType a) that do what
you want.  Note that for Eq you only need to define (==), and for Ord
you only need to define (<=) or compare.  The rest of the methods come
"for free".  See the Haskell 98 Report at www.haskell.org for details.

Also, if you use hugs, just look at it's Prelude.hs for examples of
how to define instances of Eq and Ord.

-- 
Adam P. Jenkins 
ajenkins@netway.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads