I've written this function to give the neighbours of a vertex in a graph:
import Data.Matrix (Matrix, getRow, ncols) import Data.Vector ((!)) type AdjacencyMatrix = Matrix Bool -- Input: the graph's adjacency matrix and a vertex. -- Output: the list of neighbours of that vertex. neighbours :: AdjacencyMatrix -> Int -> [Int] neighbours mat n = filter (\m -> row ! m) [0..(ncols mat)-1] where row = getRow n mat
Someone suggested here to replace my partial calls to
(!) with calls to
fold. I can't really see how this would be done. Does anyone have suggestions of how to improve this?