1 -- | The Grid module just contains the Grid type and two constructors
2 -- for it. We hide the main Grid constructor because we don't want
3 -- to allow instantiation of a grid with h <= 0.
7 import Cube (Cube(Cube))
10 -- | Our problem is defined on a Grid. The grid size is given by the
11 -- positive number h. The function values are the values of the
12 -- function at the grid points, which are distance h from one
13 -- another in each direction (x,y,z).
14 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
15 function_values :: [[[Double]]] }
19 -- | The constructor that we want people to use. If we're passed a
20 -- non-positive grid size, we throw an error.
21 make_grid :: Double -> [[[Double]]] -> Grid
22 make_grid grid_size values
23 | grid_size <= 0 = error "grid size must be positive"
24 | otherwise = Grid grid_size values
27 -- | Creates an empty grid with grid size 1.
29 empty_grid = Grid 1 [[[]]]
33 -- This is how we do a 'for' loop in Haskell.
35 cubes :: Grid -> [[[Cube]]]
37 | fvs == [[[]]] = [[[]]]
38 | head fvs == [[]] = [[[]]]
40 [[[ Cube (h g) i j k (make_values fvs i j k) | i <- [0..xsize]]
44 fvs = function_values g
45 zsize = (length fvs) - 1
46 ysize = (length $ head fvs) - 1
47 xsize = (length $ head $ head fvs) - 1
50 -- | Takes a grid and a position as an argument and returns the cube
51 -- centered on that position. If there is no cube there (i.e. the
52 -- position is outside of the grid), it will return Nothing.
53 cube_at :: Grid -> Int -> Int -> Int -> Maybe Cube
58 | i >= length (cubes g) = Nothing
59 | j >= length ((cubes g) !! i) = Nothing
60 | k >= length (((cubes g) !! i) !! j) = Nothing
61 | otherwise = Just $ (((cubes g) !! i) !! j) !! k