What you want to do is make your Vector an instance of the Num(eric)
type class.  For instance:

instance Num Vector where
  (+) v1 v2 = zipWith (+) v1 v2
  (-) v1 v2 = zipWith (-) v1 v2
  negate v1 = map negate v1
  abs v1 = map abs v1
  (*) v1 v2 = ...
  fromInteger i = ...
  signum v1 = ...

I've left the last three blank because it's unclear what should go
there.  Since (*) has type Vector->Vector->Vector (in this instance),
you can't use dot product or something like that.

signum :: Vector->Vector could be written and just return [-1], [0] or
[1], I suppose.

fromInteger :: Integer->Vector is a little harder.  Perhaps just
'fromInteger i = [fromInteger i]' would be acceptable.  Or you could
leave these undefined.

 --
 Hal Daume III                                   | [EMAIL PROTECTED]
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume


> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Liu Junfeng
> Sent: Wednesday, July 09, 2003 7:25 AM
> To: [EMAIL PROTECTED]
> Subject: How overload operator in Haskell?
> 
> 
> I've learned haskell months, but I still can't understand the 
> type system very well.
> I can only write:
> -----------------------------------
> type Vector = [Double]
> vadd,vsub :: Vector->Vector->Vector
> v1 `vadd` v2 = zipWith (+) v1 v2
> v1 `vsub` v2 = zipWith (-) v1 v2
> svmul :: Double->Vector->Vector
> s `svmul` v = map (s*) v
> ------------------------------------
> Tough it works, it is not convenient to use. How to create a 
> Vector type and overload 
> mathematical operators? 
> Thanks for your help. 
> 
> Liu Junfeng
> [EMAIL PROTECTED]
> 
> 
> 
> _______________________________________________
> Haskell mailing list
> [EMAIL PROTECTED]
> http://www.haskell.org/mailman/listinfo/haskell
> 
_______________________________________________
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell

Reply via email to