battleship

Safe HaskellSafe

Matrix

Description

This trusted module implements the small matrices that are used to represent game boards. Matrices are indexed by lowercase letters.

Synopsis

Documentation

type Matrix a = [[a]]

A matrix is just a list of lists.

valid :: Int -> Int -> Matrix a -> Bool

valid ht wid rows tests whether ht (height) and wid (width) are both at least 1 and no more than 26, and that rows is a list of ht lists, each of length wid. We say that a value rows of type Matrix a is valid iff valid ht wid rows for some ht and wid.

const :: Int -> Int -> a -> Matrix a

const ht wid x returns a list of ht rows, each consisting of a length-wid list all of whose elements are x; it raises an exception if ht and/or wid are outside of the range 1 .. 26.

sub :: Int -> Int -> Matrix a -> Int -> Int -> a

sub ht wid rows i j raises an exception if valid ht wid rows is False or i < 0 or i >= ht or j < 0 or j >= wid; otherwise, it returns the jth element of the ith element of rows.

update :: Int -> Int -> Matrix a -> Int -> Int -> a -> Matrix a

update ht wid rows i j x raises an exception if valid ht wid rows is False or i < 0 or i >= ht or j < 0 or j >= wid; otherwise, it returns the matrix that's the same as rows except that the jth element of the ith element of this matrix is x.

map :: (a -> b) -> Matrix a -> Matrix b

map f rows transforms rows by applying f to each element of each of its elements.

mapM :: Monad m => (a -> m b) -> Matrix a -> m (Matrix b)

Monadic version of map: works through the rows from top to bottom, and through each row's cells from left to right.

mapPos :: (Int -> Int -> a -> b) -> Matrix a -> Matrix b

Like map, but the function f to be mapped over the matrix is also called with initial arguments i and j, consisting of the row and column, respectively, of the cell to be transformed.

transpose :: Int -> Int -> Matrix a -> Matrix a

transpose ht wid rows raises an exception if valid ht wid rows is False; otherwise, it transposes rows (swapping rows and columns); the result, cols, will satisfy valid wid ht cols.