
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

On Fri, Feb 6, 2015 at 10:18 AM, Roelof Wobben
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.
You need to distinguish three "zero" cases: - the value is itself zero - leading zero (i.e. you hit the end of available digits) - interior zero The middle one is your end case --- but this will give you no output if the value you put in initially is zero, since you're already at the end case. So you need to treat an initial value of 0 specially somehow to get output for it. So, either an explicit conditional or a setup where the public function handles an explicit zero and otherwise defers to what you have, which becomes an internal/hidden function (via where or let). -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
participants (2)
-
Brandon Allbery
-
Roelof Wobben