Need Haskell programming Help. How to separate and label clusters ( 4 direction connected components) in a list of list number (2-d array) and replace all numbers in a specific cluster to 1 (I need to...




Need Haskell programming Help. How to separate and label clusters ( 4 direction connected components) in a list of list number (2-d array) and replace all numbers in a specific cluster to 1 (I need to write this in haskell.) For example, I have a list of string number ( 2d array) like this: ["122221111211", "211121221121", "221211222211", "111212122112", "112121212222", "112222121211", "211211221112"] the cluster will be seperate like this: (the way to label the clusters is like the flood fill, if it connects to their neighbor by up-down-left-right direction, they will be grouping in the same cluster. we go from top left to bottom right.) And I need to replace all numbers of the cluser 21st (which is just number 2) to 1. So the result will be ​ ["122221111211", "211121221121", "221211222211", "111212122112","112121212222", "112222121211", "111211221112"] I have some haskell starter code: -- "game" will be the list of list of numbers (example above) and "move" will be the specific cluster number (cluster 21st above) that needs to be changed the numbers in it onePlayerOneMove :: [[Char]] -> Int -> [[Char]] onePlayerOneMove game move = game findNeighbors :: [[Char]] -> [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)]-- findNeighbors game positionsToCheck positionsChecked findNeighbors _ [] _ = [] findNeighbors game (position:positions) checked = position:(findNeighbors game (positions++usefulNeighbors) (position:checked))

where positionNeighbors = generateNeighbors position game usefulNeighbors = [ n | n <-> [[a]] -> [(Int,Int)] generateNeighbors (r,c) game = neighbors

where nRows = length nCols = length (head game) above = [(row,col)|row<-[c],r>0] below = [(row,col)|row<(nrows-1)]><-[(c-1)],c>0]

right = [(row,col)|row<(ncols-1)]>

get :: [[a]] -> (Int,Int) -> a -- get game position = element at that position in the game

get (row:_) (0,c) = getCol row c get (_:rows) (r,c) = get rows ((r-1),c) getCol :: [a] -> Int -> a -- getCol row position = element at that position in the row getCol (col:_) 0 = col getCol (_:cols) c = getCol cols (c-1) set :: [[a]] -> (Int,Int) -> a -> [[a]] -- set game position element = game with position replaced by element set (row:rows) (0,c) el = (setCol row c el):rows

set (row:rows) (r,c) el = row:(set rows (r-1,c) el) setCol :: [a] -> Int -> a -> [a] setCol (_:cols) 0 el = el:cols setCol (col:cols) c el = col:(setCol cols (c-1) el) generatePositions :: [[a]] -> [(Int,Int)] -- generate all positions for a given game -- 8x7 game should generate (0,0), (0,1), ..., (0,6), (1,0), ..., (7,6) generatePositions game = position where nRows = length game nCols = length (head game) positions = [(r,c)|r



May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here