Haskell编码问题

时间:2017-09-28 12:19:45

标签: haskell

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))

我不明白为什么这段代码会返回以下错误,因为图层完全适合网络。即使我将网络定义为,它也无法编译 矩阵矢量网|无

这是错误:

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))
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 个答案:

答案 0 :(得分:4)

您已将Layer两次定义为两个不同的东西:

  • 作为数据构造函数(在data Network = Layer | None
  • 作为类型别名(在type Layer = (Matrix,Vector,Network)

那些不一样。

createNetwork的第二行,你返回一个三元组(一个三元组)。虽然它等同于类型别名Layer,但它不是Network

您应该将类​​型定义更改为以下内容:

data Network = L Layer | None

type Layer = (Matrix,Vector,Network)

然后你应该能够写出类似的内容:

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

我现在不在Haskell编译器附近,所以我无法检查,但如果这不起作用,请告诉我,我会看到我能做什么。