1
votes

I am new to Haskell. I have a Neuron data type, which owns a list of Double values:

data Neuron = Neuron [Double]
      deriving  (Eq, Read, Show)

I am trying to do the sum of each element in the list owned by the Neuron and in the other list:

sommeNeuron :: Neuron -> [Double] -> Neuron 
sommeNeuron n1 n2 = n'
              where {
                     --n' = Neuron(zipWith (+) n1 n2);
                     n' = zip n1 n2
              }

That gives me a compile-time error:

Couldn't match expected type ‘[a]’ with actual type ‘Neuron’

1
Your two arguments have different types, so it is not a very good idea to call them n1 and n2. Let's suppose the numbers are weights, and your n2 is a list of increments for existing weights. So you can call the initial weights ws0, the increments ws1, and the weights of the result neuron ws2. So the body of your function would start like: sommeNeuron (Neuron ws0) ws1 = Neuron ws2 where {TODO must define ws2 as a function of ws0 and ws1}.jpmarinier

1 Answers

4
votes

Just need to tweak it,

sommeNeuron :: Neuron -> [Double] -> Neuron 
sommeNeuron (Neuron n1) n2  =  Neuron (zipWith (+) n1 n2)
{-
             Neuron n1     :: Neuron
                    n1     ::   [Double]
                       n2  ::        [Double]
        zipWith (+) n1 n2  ::             [Double]
Neuron (zipWith (+) n1 n2) ::                   Neuron
-}

since your data type is defined as

data Neuron = Neuron [Double]

i.e. the [Double] list is behind the "tag" Neuron. This means there's a relationship

--     data               Type
--  constructor
        Neuron   n1  ::  Neuron
    -------------------------------
                 n1  ::  [Double]
--            variable
--              name