It would probably be better to define:
Code:
data Domino = Domino { left :: Int, right :: Int }
type Hand = [Domino]
type Board = [Domino]
... rather than using a type synonym. Additionally, since dominoes can be added to either end of the Board and selected from any point in the Hand, a list probably isn't the ideal datatype — I would go for a sequence for the Board, and a bag for the Hand.
However, proceeding with the lists for the moment:
Code:
gLeft, gRight :: Domino -> Board -> Bool
gLeft _ [] = True
gLeft (Domino a b) ((Domino lm _) : _) = lm == a || lm == b
gRight d b = gLeft d (reverse b)
Here's a better implementation, using Data.MultiSet and Data.Sequence (the latter distributed with GHC, the former available on Hackage):
Code:
import qualified Data.MultiSet as Bag
import Data.Sequence (Seq (EmptyL), viewl, ViewL((:<)), viewr, ViewR((:>)))
data Domino = Domino { left :: Int, right :: Int }
type Hand = Bag.MultiSet Domino
type Board = Seq Domino
gLeft, gRight :: Domino -> Board -> Bool
gLeft _ EmptyL = True
gLeft (Domino l r) b = let ((Domino bl _) :< _) = viewl b in bl == l || bl == r
gRight _ EmptyL = True
gRight (Domino l r) b = let (_ :> (Domino _ br)) = viewr b in br == l || br == r
Bookmarks