Peter Kiss
Peter Kiss

Reputation:

Haskell coding issue

type Matrix = [[Rational]]

type Vector = [Rational]

data Network = Layer | None

type Layer = (Matrix,Vector,Network)

createNetwork :: [Matrix] -> [Vector] -> Network
createNetwork [] _ = None
createNetwork (x:xs) (y:ys) = (x,y,(createNetwork xs ys))

I dont understand why does this code returns the following error, since a layer is exactly fitting for a Network. It doesn't compile even if i define Network as Matrix Vector Network | None

This is the error :

neuro.lhs:17:33: error:
    * Couldn't match expected type `Network'
                  with actual type `(Matrix, Vector, Network)'
    * In the expression: (x, y, (createNetwork xs ys))
      In an equation for `createNetwork':
          createNetwork (x : xs) (y : ys) = (x, y, (createNetwork xs ys))
   |
17 | > createNetwork (x:xs) (y:ys) = (x,y,(createNetwork xs ys))
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Upvotes: 0

Views: 44

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233150

You've defined Layer twice, as two different things:

  • As a data constructor (in data Network = Layer | None)
  • As a type alias (in type Layer = (Matrix,Vector,Network))

Those are not the same.

In the second line in createNetwork, you return a triad (a 3-tuple). While it's equivalent to the type alias Layer, it's not a Network.

You should probably change your type definitions to something like this:

data Network = L Layer | None

type Layer = (Matrix,Vector,Network)

Then you should be able to write something like:

createNetwork :: [Matrix] -> [Vector] -> Network
createNetwork [] _ = None
createNetwork (x:xs) (y:ys) = L (x,y,(createNetwork xs ys))

I'm not nearby a Haskell compiler right now, so I can't check, but let me know if that doesn't work, and I'll see what I can do.

Upvotes: 4

Related Questions