[project @ 1999-01-23 18:07:42 by sof]
[ghc-hetmet.git] / ghc / tests / programs / sanders_array / Main.hs
1 {-
2 From: Paul Sanders <psanders@srd.bt.co.uk>
3 To: partain
4 Subject: A puzzle for you
5 Date: Mon, 28 Oct 91 17:02:19 GMT
6
7 I'm struggling with the following code fragment at the moment:
8 -}
9
10 import Array -- 1.3
11 import Ix    -- 1.3
12
13 conv_list :: (Ix a, Ix b) => [a] -> [b] -> [[c]] -> Array (a,b) c -> Array (a,b) c
14 conv_list [] _ _ ar = ar
15 conv_list _ _ [] ar = ar
16 conv_list (r:rs) cls (rt:rts) ar
17       = conv_list rs cls rts ar'
18         where ar' = conv_elems r cls rt ar
19
20 conv_elems :: (Ix a, Ix b) => a -> [b] -> [c] -> Array (a,b) c -> Array (a,b) c
21 conv_elems row [] _ ar = ar
22 conv_elems _ _ [] ar = ar
23 conv_elems row (col:cls) (rt:rts) ar
24       = conv_elems row cls rts ar'
25         where ar' = ar // [((row,col), rt)]
26
27 ar :: Array (Int, Int) Int
28 ar = conv_list [(1::Int)..(3::Int)] [(1::Int)..(3::Int)] ar_list init_ar
29      where init_ar = array (((1::Int),(1::Int)),((3::Int),(3::Int))) []
30
31
32 ar_list :: [[Int]] -- WDP
33 ar_list = [[1,2,3],
34            [6,7,8],
35            [10,12,15]]
36
37 main = putStrLn (show ar)
38
39 {-
40 What it tries to do is turn a list of lists into a 2-d array in an incremental
41 fashion using 2 nested for-loops. It compiles okay on the prototype compiler
42 but gives a segmentation fault when it executes. I know I can define in the
43 array in one go (and I have done) but, for my piece of mind, I want to get this
44 way working properly.
45
46 Is it a bug in the prototype or is there a glaringly obvious error in my code
47 which I've been stupid to spot ????
48
49 Hoping its the latter,
50
51 Paul.
52 -}