
Hello, I have figured out how I can make from 123 [1,2,3] I did it this way : -- | convert a number to a array in pieces where a negative number will be a empty array. toDigits :: Integer -> [Integer] toDigits n | n <= 0 = [] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10] but now when I do toDigits 0 , I see [] as output where I was expected [0] But when I do toDigits 100 I see the right output [ 1,0,0] which surprises me because I tought that with the first 0 there will be a [] Or is the n the second time [0,0] and the thirth time [0] So it will be like this : toDigits 100 to Digits [1] ++ [ 0,0] toDigits [1] ++ [0] ++ [0] which leads to [1,0,0] Roelof

On Fri, Feb 6, 2015 at 6:25 PM, Roelof Wobben
Hello,
I have figured out how I can make from 123 [1,2,3]
I did it this way :
-- | convert a number to a array in pieces where a negative number will be a empty array. toDigits :: Integer -> [Integer] toDigits n | n <= 0 = [] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
but now when I do toDigits 0 , I see [] as output where I was expected [0]
But when I do toDigits 100 I see the right output [ 1,0,0] which surprises me because I tought that with the first 0 there will be a []
Or is the n the second time [0,0] and the thirth time [0] So it will be like this :
toDigits 100
to Digits [1] ++ [ 0,0] toDigits [1] ++ [0] ++ [0]
which leads to [1,0,0]
toDigits 100 toDigits 10 ++ [0] toDigits 1 ++ [0] ++ [0] [1] ++ [0] ++ [0] [1,0,0] toDigits 0 [] -- because first guard clause 0 is valid input. It should be dealt different from negative number. -- YCH

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

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@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

Roelof Wobben schreef op 6-2-2015 om 10:25:
Hello,
I have figured out how I can make from 123 [1,2,3]
I did it this way :
-- | convert a number to a array in pieces where a negative number will be a empty array. toDigits :: Integer -> [Integer] toDigits n | n <= 0 = [] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
but now when I do toDigits 0 , I see [] as output where I was expected [0]
But when I do toDigits 100 I see the right output [ 1,0,0] which surprises me because I tought that with the first 0 there will be a []
Or is the n the second time [0,0] and the thirth time [0] So it will be like this :
toDigits 100
to Digits [1] ++ [ 0,0] toDigits [1] ++ [0] ++ [0]
which leads to [1,0,0]
Roelof
Hmm, this is also not working : -- | convert a number to a array in pieces where a negative number will be a empty array. toDigits :: Integer -> [Integer] toDigits 0 = [0] toDigits n | n < 0 = [] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10] -- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n <= 0 = [] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10) -- | The main entry point. main :: IO () main = do print $ toDigits 100 print $ toDigitsRev 100 print $ toDigits 0 print $ toDigitsRev 0 print $ toDigits (-17) print $ toDigitsRev (-17) you get a zero too much when you use a number above 0 .

On Fri, Feb 6, 2015, at 01:25 AM, Roelof Wobben wrote:
but now when I do toDigits 0 , I see [] as output where I was expected [0]
In our written notation, "0" is a special case: it's the only integer written with a leading zero. So don't be too surprised if your code requires a special case for it. -Karl

Brilliantly succinct answer!
And the programming task that started this thread is a nice (though
microscopic) illustration of the culture shock when our tidy, jewel-like
programming models come to grips with the messy, non-systematic nature of
many application domains.
Thanks for both.
-jn-
On Fri, Feb 6, 2015 at 10:59 PM, Karl Voelker
On Fri, Feb 6, 2015, at 01:25 AM, Roelof Wobben wrote:
but now when I do toDigits 0 , I see [] as output where I was expected [0]
In our written notation, "0" is a special case: it's the only integer written with a leading zero. So don't be too surprised if your code requires a special case for it.
-Karl _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato
participants (5)
-
Joel Neely
-
Karl Voelker
-
Marcin Mrotek
-
Roelof Wobben
-
YCH