
Hello Thomas, Thomas Nelson schrieb:
I'm brand new to haskell and I'm having trouble using classes. The basic idea is I want two classes, Sine and MetaSine, that are both instances of ISine. This way I can use the act method and recurse through the metasines and sines.
That looks too much like object oriented design and too less like haskell for me. Have you considered using a standard algebraic type for all variantes of sines? About your code:
class ISine a where period :: a -> Integer offset :: a -> Integer threshold :: a -> Integer act :: (ISine b) => Integer -> a -> b on :: Integer -> a -> Bool --on needs offset, period, threshold on time self = (mod (time-(offset self)) (period self)) < (threshold self)
on is the same for alle instances? then don't include it in the type class, but provide it as polymorphic "helper" function: on :: (ISine a) => Integer -> a -> Bool on time self = (mod (time - (offset self)) (period self)) < (threshold self) and don't use self, because it could prevent you from forgetting that haskell is not object oriented.
data Sine = Sine { period :: Integer, offset :: Integer, threshold :: Integer, letter :: String }
You are not allowed to use period, offset and threshold as selector names, because they are already used as members of class ISine.
instance Sine ISine where act time (Sine self) |on time self = [letter self] |otherwise = []
You should provide definitions for all members of class ISine in this instance declaration. there are no automatic use of the like-named selectors. (in fact, the naming is illegal as pointed out above and by the compiler). Same for MetaSine, of course. I strongly suggest to ignore type classes and instances for a while and start learning about the way haskell represents data as algebraic data types. What you tried seems comparable to using a c++ template where a simple c function would do. Maybe working through a tutorial before trying to implement your own ideas could be helpfull, too. Tillmann