Implementing and indexing 2 dimen arrays

I am writing a function, It's my first Haskell program in output function I will be working on [[3,4],[1,2,3,2],[3,9,9,4],[8,3,3,4] type structures I'd like to know how to index a particular element at run-time as I don't know on which element of which sublist I am at the moment in pullHelper function which starts with values !! ind p xs !! in C it would look something like values[][10]={{....}.{....}..}; int index[4]={3,4,6,7}; int i; i for(i = 0; i<4; i++) { int c = index[i]; if(values[i][c-1] == 0) /* do something */ else /*other things*/ } Any help would be appreciated --Stack for the digits of numbers, a modulo b, stack :: Int->Int->[[Int]] stack x y = reverse (split (subStack x y)) where --Split the digits further and constructs lists of lists split :: [Int]->[[Int]] split xs | length xs == 0 = [] | otherwise = (reverse (subStack (head xs) 10)): (split (tail xs)) subStack :: Int->Int->[Int] subStack x y | x == 0 = [] | otherwise = (mod x y):(subStack (div x y) y) -- It should work for 999999999 range but since I am using -- Int maximum is 2^31-1 output :: Int->[Char] output n = loop (stack n 10000) where -- goes through sets of at most 4 digits in the list, loop loop :: [[Int]]->[Char] loop xs | length xs == 0 = [] | otherwise = (strCon (head xs)) : (loop (tail xs)) strCon :: [Int]->[Char] strCon xs | length xs == 0 = [] | otherwise = (pullStr (head xs)) : (strCon (tail xs)) pullStr :: Int->[Char] pullStr s | s == 0 = [] | otherwise = pullHelper s --Helping to pull elements from the set, nested if pullHelper :: Int->[Char] pullHelper p | values !! ind p xs !! p - 1 == 0 = ones !! p - 1 ++ values !! ind p xs !! 0 | otherwise = values !! ind p xs !! p - 1 --Nasty !

Alex Gontcharov wrote:
I am writing a function, It's my first Haskell program in output function I will be working on [[3,4],[1,2,3,2],[3,9,9,4],[8,3,3,4] type structures I'd like to know how to index a particular element at run-time as I don't know on which element of which sublist I am at the moment in pullHelper function which starts with values !! ind p xs !!
Note that Haskell has arrays as well as lists. Arrays may be better
suited to your problem.
--
Glynn Clements

I'd like to know how to index a particular element at run-time as I don't know on which element of which sublist I am at the moment in pullHelper function which starts with values !! ind p xs !!
It's maybe worth pointing out that few Haskell programs use indexing. It's far more common to see programs use pattern matching, array comprehensions, or a higher order function than to see indexing of lists. For example, I'd write: map show xs instead of [ show (xs !! i) | i <- [0..length xs -1] (which is about the closest translation of a C for loop indexing an array). or, if you need the index of a value in an array, you might write something like: [ i*x | (i,x) <- zip [0..] xs ] Not only is is usually shorter to avoid indexing but it is also less error prone. In C, Pascal, etc. it is really easy to walk off the end of an array and the error isn't caught until (at best) runtime. If you use pattern matching, etc. the error is harder to make and tends to be caught at compile time. Of course, sometimes you do need random access. For that, you should use arrays. -- Alastair Reid www.haskell-consulting.com
participants (3)
-
Alastair Reid
-
Alex Gontcharov
-
Glynn Clements