[Haskell-cafe] Question about classes from a Haskell newbie.

Lemmih lemmih at gmail.com
Mon May 23 12:11:45 EDT 2005


On 5/23/05, Jeff.Harper at handheld.com <Jeff.Harper at handheld.com> wrote:
>  
> Hi, 
>  
> I'm new to Haskell and even newer to this list.   I have over 20 years of
> experience in C or C++. 
>  
> For fun, I decided to write a Fourier transform in Haskell.  It would be
> convenient if I had a function that converts any real or complex number into
> a complex number.  My attempt at doing this is below.  Hugs produces errors
> when I try to load this code. 
>  
> Essentially, I'm trying to define a class called ConvertibleToComplex. 
> There is a function in this class called toComplex.  toComplex has the type:
>  
> class ConvertibleToComplex a where 
>    toComplex :: RealFloat b => a -> Complex b 

I don't think you want an arbitrary type 'b'. Try: (pass -98 to Hugs
or -fglasgow-exts to GHCi when loading this code)

import Complex

class ConvertibleToComplex a b | a -> b where
   toComplex :: RealFloat b => a -> Complex b

instance ConvertibleToComplex Float Float where
        toComplex f = f :+ 0

instance ConvertibleToComplex Double Double where
        toComplex d = d :+ 0

instance ConvertibleToComplex (Complex a) a where
        toComplex c = c

-- 
Friendly,
  Lemmih


More information about the Haskell-Cafe mailing list