[Haskell-beginners] I need advice on design

Amy de Buitléir amy at nualeargais.ie
Mon Dec 28 12:14:53 EST 2009


I'm building a library of components for artificial neural networks.
I'm used to object-oriented languages, so I'm struggling a bit to
figure out how to do a good design in a functional programming
language like Haskell.

Q1: I've come up with two designs, and would appreciate any advice on
improvements and what approach to take.

===== Design #1 =====
class Neuron n where
  activate :: [Double] -> n -> Double
  train :: [Double] -> Double -> n -> n

...and then I would have instances of this typeclass. For example:

data Perceptron = Perceptron {
        weights :: [Double],
        threshold :: Double,
        learningRate :: Double
      } deriving (Show)

instance Neuron Perceptron where
  activate inputs perceptron = ...
  train inputs target perceptron = ...

The disadvantage of this approach is that I need to define and name
each instance of neuron before I can use it. I'd rather create a
neuron on-the-fly by calling a general-purpose constructor and telling
it what functions to use for activation and training. I think that
would make it easier to re-use activation and training functions in
all sorts of different combinations. So I came up with...

===== Design #2 =====
data Neuron =
    Neuron {
        weights :: [Double],
        activate :: [Double] -> Double,
        train :: [Double] -> Double -> Neuron
      }

Q2: I thought there might be some way to define a function type, but
the following doesn't work. Is there something along these lines that
would work?

type activationFunction = [Double] -> Double


More information about the Beginners mailing list