Getting a type mismatch error with the Haskell zipWith function

138 Views Asked by At

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

There are 1 best solutions below

0
On BEST ANSWER

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