[Haskell-cafe] recursion problem part 2

Roelof Wobben r.wobben at home.nl
Fri Feb 6 15:18:36 UTC 2015


Hello,

I partly solved the problem of cis194 of turning 123 into [1,2,3]

But I ran into a problem I cannot solve .


When I have this :

toDigits :: Integer -> [Integer]
toDigits n
    | n < 0 = []
    | n == 0 = []
    | otherwise =  toDigits (n `div` 10) ++ [n `mod` 10]

toDigits 123 gives [ 1,2,3]
but toDigits 0 gives [] where I expect to be [0]

So I change the code to this :

toDigits :: Integer -> [Integer]
toDigits n
    | n < 0 = []
    | n == 0 = [0]
    | otherwise =  toDigits (n `div` 10) ++ [n `mod` 10]

Now toDigits 0 gives [ 0]
but toDigits 123 gives now [ 0, 1 ,2, 3]  where I expect to be [1,2,3]


Can someone give me a tip how to solve this
I have asked this on the beginners ML but the only answer I get was 
someone who was using some if -then statement and I think I can be 
solved without.

Roelof



More information about the Haskell-Cafe mailing list