Reputation: 57
I am in trouble creating an haskell function which convert a Int from base 10 to base 8 without the use of recursion. all library functions allowed
the recursive version I have in mind uses 'div' and 'mod', but without recursion I have no idea. I hope anyone has any idea how to make it work
edit: I know how the foldr work in principle but I can't still write the function down, especially because I know the foldr produces one result number, but I need every intermediate result of 'mod'. foldr ('mod') 8 [????].
Upvotes: 0
Views: 143
Reputation: 476534
Iteration in Haskell is done through recursion, but that does not mean you need to write the recursion yourself.
Likely they want you to use a higher-order function like unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
that will implement the recursion for you.
You thus can use unfoldr
where you pass a function b -> Maybe (a, b)
as first parameter that produces a Just (y, x)
where the x
is the value for the next iteration, and the y
the "emitted" value.
Upvotes: 3