Search code examples
haskellnewtype

couldn't match expected type (newtype)


I'm working on a function

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

using these types

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

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

type Cell = (Int, Int)

createTypeBoard (2,2) Mine should be: Board [((1,1), X), ((1,2), X), ((2,1), X), ((2,2), X)]

(X is the show instance of Mine)

my idea was to use zip:

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

but I keep getting an error

• 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 basically is [(Cell, CellType)] so I don't really get what the problem is :(


Solution

  • You declared Board with newtype, but you're trying to use it as if you declared it with type. Two choices to fix it:

    1. Declare board like this instead: type Board = [(Cell, CellType)]
    2. Use the Board data constructor in createTypeBoard, like this: createTypeBoard (a, b) c = Board $ zip (createCells (a, b)) [c]

    For more information on the difference between type and newtype, which may affect your decision on which fix to go with, see Haskell type vs. newtype with respect to type safety and/or The difference between type and newtype in Haskell.