user2878641
user2878641

Reputation:

Haskell Data Types and type classes

Due to a former question, I've been studying hard data types and type classes in haskell, and I want to know if a have a certain class and a data type,

For example:

class Example a where

function :: a -> Int

and

data Tree a = EmptyTree
        | Node a (Tree [a]) (Tree [a]) deriving (Show, Read, Eq)

How can I use the function in the class example in the data type tree?

I think it has something to do with instances, but is this correct?

instance Example where
    function :: a -> Int, and then i define the function here? 

Can you give me an example?

Upvotes: 1

Views: 360

Answers (2)

Ingo
Ingo

Reputation: 36339

Judging from the other question you asked lately, I think you want something like this:

class Order a where
    order :: a -> Int

insert :: Order k => k -> Tree k -> Tree k
insert key tree
    | order key == 0 = ..do work for items of order 0 ...
    | otherwise      = ..do work for items of higher order ...

Upvotes: 1

Lee
Lee

Reputation: 144206

Your instance implementation should look like:

instance Example (Tree a) where
  function EmptyTree = ...
  function (Node val left right) = ...

Upvotes: 1

Related Questions