[Haskell-beginners] Custom type classes

Imants Cekusins imantc at gmail.com
Mon Jan 25 07:42:10 UTC 2016


Hello Daniel,

it works with these tweaks:

-- begin

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
module TupInst where


data Tuple3 a b c = Tuple3 a b c deriving (Show)

data Tuple2 a b = Tuple2 a b deriving (Show)

class Indexable idx a where
   first :: idx -> a


instance Indexable (Tuple2 a b) a where
   first (Tuple2 a0 b0) = a0


instance Indexable (Tuple3 a b c) a where
   first (Tuple3 a0 b0 c0) = a0

-- end

call it in ghci like this:

first $ Tuple3 (1::Int) 'a' False::Int


More information about the Beginners mailing list