loop in C , recursion in haskell

Hi, I am new to haskell and would look to write a function equivalent to the following loop in C int value = 500000; int part_stack[4]; int *part_ptr = part_stack; for (; value; value /= 10000) *part_ptr++ = value % 10000; Thanks

On Thu, Mar 25, 2004 at 01:38:44AM +1100, Alex Gontcharov wrote:
Hi,
I am new to haskell and would look to write a function equivalent to the following loop in C
int value = 500000; int part_stack[4]; int *part_ptr = part_stack; for (; value; value /= 10000) *part_ptr++ = value % 10000;
part_stack :: [Int] part_stack = [0,50] Note that I've performed a memoization optimization--this makes the code both smaller, faster and easier to read! :P But seriously, the C code doesn't do anything. Why do you want to translate it? (Unless it's homework...) -- David Roundy http://www.abridgegame.org

Alex, AG> I am new to haskell and would look to write a function AG> equivalent to the following loop in C AG> int value = 500000; AG> int part_stack[4]; AG> int *part_ptr = part_stack; AG> for (; value; value /= 10000) AG> *part_ptr++ = value % 10000; What about this?
part :: Int -> Int -> [Int] part k n = unfoldr f k where f 0 = Nothing f m = Just (m `mod`n, m `div` n)
part 500000 10000, for instance, produces the [0, 50]. Here, unfoldr is the dual of the function foldr from the Prelude.
unfoldr :: (b -> Maybe (a,b)) -> b -> [a] unfoldr f b = case (f b) of Nothing -> [] Just (a,b) -> a : unfoldr f b
HTH, Stefan
participants (3)
-
Alex Gontcharov
-
David Roundy
-
Stefan Holdermans