Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell List to an Array

Working on a chess like move calculator in Haskell and I'm running in to a little problem for an assignment I'm forced to use Data.Array to save my pieces in with a Position defined by a Char and an Int. Now I have a list of pieces as input with writeToArray and I've tried a lot but I keep getting errors, does anyone know what I'm doing wrong?

Here is the code that it's about:

data Squares = Squares {array :: Array Pos Piece}
data Pos = Pos Char Int deriving (Eq, Ord, Ix)
data Piece = Piece { piecetype :: PieceType, color :: PieceColor } | Empty

writeToArray :: [Piece] ->(Array Pos Piece)
writeToArray (x:xs) = (((Pos 'a' 1), (Pos 'h' 8)) [((Pos char int), x) | char <- ['a'..'h'], int <- [1..8]])` : writeToArray (xs)

And the error it gives:

Couldn't match expected type `Array Pos Piece' with actual type '[a0]'

All help is appreciated greatly

like image 576
M4st3rM1nd Avatar asked Oct 22 '25 23:10

M4st3rM1nd


1 Answers

Fixed it!

changed the writeToArray function to this:

writeToArray :: [Piece] ->(Array Pos Piece)
writeToArray  list = Data.Array.array (Pos 'a' 1, Pos 'h' 8) (zip  [(Pos char int)| char <- ['a'..'h'], int <- [1..8]] list)

may it help other people with this problem ^_^

like image 170
M4st3rM1nd Avatar answered Oct 25 '25 17:10

M4st3rM1nd