Norbert Melzer schreef op 5-2-2015 om 18:16:


Am 05.02.2015 17:29 schrieb "Marcin Mrotek" <marcin.jan.mrotek@gmail.com>:
>
> Well, toDigits is always finishing its work after dividing the input number once, and always returns lists of at most one element. You need it to call itself recursively until it's done, and append the calculated digit to the result:
>
> toDigits :: Integer -> [Integer]
> toDigits n
>   | n <= 0 = []
>   | otherwise = n `mod` 10 : toDigits (n `div` 10)

This doesn't feel right:

toDigits 10 = 0 : toDigits 1 = 0 : 1 : toDigits 0 = 0 : 1 : [] = [0,1]

I'd expect the result to be [1,0]...

>
> The <= is necessary, otherwise the function would loop infinitely on 0. This version will print the digits in reverse, so you might move the code to a local function in toDigits, and make toDigit call it and then reverse the result.
>
> I'd strongly recommend reading a Haskell tutorial (for example http://learnyouahaskell.com ) to learn the basics.
>
> Kind regards,
> Marcin Mrotek
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


Thanks all,

I see that I run into a old problem of me.

I have read the learnyou haskell book and thougth I understand it.

But then a old problem arisse again. I understand what I read but find it difficult to use in real world like exercises.

Roelof