list - Iterate over all (Row, Column) possibilities? -


this tictactoe implementation:

data row    = | b | c  deriving (show, read, eq, ord, enum, bounded) data column = x | y | z  deriving (show, read, eq, ord, enum, bounded) type pos = (row, column) data player = player string data field = field [(pos, player)] initialfield :: field initialfield = field [] 

as can see, initialfield empty list , players make moves, (pos, player) tupels added list. far good. have trouble writing possiblemoves :: field -> [pos] function empty fields. how iterate on row, column possibilities in haskell? feeling approach wrong, i'm new haskell have no better idea.

we can positions list comprehensions (see other answers)

positions :: [pos] positions = [(r,c) | r <- [a,b,c], c <- [x,y,z]] 

and plays map

occupied :: field -> [pos] occupied (field l) = fmap fst l 

then can define possiblemoves (you need import data.list \\, list difference):

possiblemoves :: field -> [pos] possiblemoves f = positions \\ (occupied f) 

a more compact version takes advantage of list comprehension constraints:

possiblemoves :: field -> [pos] possiblemoves (field l) = [(r,c) | r <- [a,b,c], c <- [x,y,z], (r,c) `notelem` occupied]   occupied = fmap fst l 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -