[Haskell-beginners] How to correctly define data types?

Davi Santos dps.abc at gmail.com
Mon Jul 18 19:26:33 CEST 2011


Hello (first post),
I have spent so many time learning typeclasses and thinking it was part of
Haskell essence... and suddenly I read the discussion "[Haskell-beginners]
Can fields in a record be optional?".
So typeclasses are not always recomended?

I'm implementing a Machine Learning framework and I am in a sort of related
dilemma.

I found three ways of implementing the same distance function between
"examples" (aka "attribute vectors" or simply "Float vectors" for mere
mortals :) ):

[obs: "Example" datatype will be added more fields later]

--------------first------------------------------------
module ML where

data Example =
     Example [Float] deriving (Show)

class ExampleClass a where
     (distance) :: a →  a →  Float

instance ExampleClass Example where
   (Example atts1) distance (Example atts2) =
        sqrt $ sum $ map (λ(x, y) →  (x-y)↑2) $ zip atts1 atts2
=================================


--------------second------------------------------------
module ML where

data Example =
    Example {attributes :: [Float]} deriving (Show)

distance :: Example →  Example →  Float
distance ex1 ex2 =
        sqrt $ sum $ map (λ(x, y) →  (x-y)↑2) $
                zip (attributes ex1) (attributes ex2)
=================================


--------------third------------------------------------
module ML where

data Example =
       Example [Float] deriving (Show)

distance :: Example →  Example →  Float
distance (Example att1) (Example att2) =
      sqrt $ sum $ map (λ(x, y) →  (x-y)↑2) $
                zip (att1) (att2)
=================================


All three reserves the word "distance" for itself and the second reserves
also the word "attributes".
How could I implement the module ML and which would be the best way  to set
"attributes" outside the module?

Thanks


Davi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110718/7fa7f9fd/attachment.htm>


More information about the Beginners mailing list