【问题标题】:couldn't match expected type (newtype)无法匹配预期类型(新类型)
【发布时间】:2019-12-25 09:20:55
【问题描述】:

我正在处理一个函数

createTypeBoard :: (Int, Int) -> CellType -> Board

使用这些类型

newtype Board = Board [(Cell, CellType)] deriving(Eq)

data CellType  = Mine                
           | Flag                    
           | Void                     
           | Enumerated Int          
           | Undiscovered             
           deriving Eq

type Cell = (Int, Int)

createTypeBoard (2,2) 我的应该是:Board [((1,1), X), ((1,2), X), ((2,1), X), ((2,2 ), X)]

(X 是我的节目实例)

我的想法是使用 zip:

createTypeBoars (a, b) c = zip (createCells (a, b)) [c]

但我不断收到错误

• Couldn't match expected type ‘Board’
              with actual type ‘[(Cell, CellType)]’
• In the expression: zip (createCells (a, b)) [Flag]
  In an equation for ‘createTypeBoard’:
      createTypeBoard (a, b) Flag = zip (createCells (a, b)) [Flag]

Board 基本上是 [(Cell, CellType)] 所以我真的不明白问题是什么:(

【问题讨论】:

    标签: haskell newtype


    【解决方案1】:

    您使用newtype 声明了Board,但您尝试使用它就像您使用type 声明它一样。修复它的两种选择:

    1. 像这样声明boardtype Board = [(Cell, CellType)]
    2. createTypeBoard 中使用Board 数据构造函数,如下所示:createTypeBoard (a, b) c = Board $ zip (createCells (a, b)) [c]

    有关typenewtype 之间差异的更多信息,这可能会影响您决定使用哪个修复程序,请参阅Haskell type vs. newtype with respect to type safety 和/或The difference between type and newtype in Haskell

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      相关资源
      最近更新 更多