[Haskell-cafe] Implementing and indexing 2 dimen arrays
Alex Gontcharov
alexg005 at hotmail.com
Fri Mar 26 03:34:23 EST 2004
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 !
More information about the Haskell-Cafe
mailing list