[Haskell-beginners] recursion problem.

Roelof Wobben r.wobben at home.nl
Fri Feb 6 13:44:19 UTC 2015


Marcin Mrotek schreef op 6-2-2015 om 11:27:
> Ah, sorry, I didn't think of that when I responded to your other
> thread. You can always insert a check before recursion:
>
> toDigits :: Integer -> [Integer]
> toDigits n
>   | n < 0 = []
>   | otherwise =  (if n' > 0 then toDigits n' else []) ++ [n `mod` 10]
>       where n' = n `div` 10
>
> Regards,
> Marcin Mrotek
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>

Hello,

Can you explain to me why we need a n'  and a where here.

I tried the same solution here :

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

but I could not make it work.

Roelof



More information about the Beginners mailing list