
24 Mar
2004
24 Mar
'04
3:38 p.m.
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