public
Description: miscellaneous opensource stuf
Homepage:
Clone URL: git://github.com/osfameron/misc-opensource.git
100644 30 lines (23 sloc) 0.623 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- we want to fold something like this
g = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
    ]
 
-- into a Cell { value=1, right=(the Cell with value 2), down=(the cell with value 4) }
 
data Cell a = Cell {
    value :: a,
    down :: Cell a,
    right :: Cell a
    }
    | Nil
    deriving Show
 
right' :: Cell a -> Cell a
right' c@(Cell {}) = right c
right' Nil = Nil
 
mkRow :: [a] -> Cell a -> Cell a
mkRow xs c = foldr mkCell Nil valAndDown
    where valAndDown = zip xs (iterate right' c)
          mkCell (v,d) r = Cell { value=v, down=d, right=r }
 
mkGrid :: [[a]] -> Cell a
mkGrid = foldr mkRow Nil