Split a number into its digits with Haskell?

You'll probably want to reverse the list of numbers if you want to treat the most significant digit first. Converting the number into a string is an impaired way of doing things.

You'll probably want to reverse the list of numbers if you want to treat the most significant digit first. Converting the number into a string is an impaired way of doing things.135 `div` 10 = 13 135 `mod` 10 = 5 Or, if you want the full code to get the digits: digs 0 = digs x = digs (x `div` 10) ++ x `mod` 10 Or in reverse: digs 0 = digs x = x `mod` 10 : digs (x `div` 10) This treats 0 as having no digits. A simple wrapper function can deal with that special case if you want to.

Does it work for negative numbers? I haven't tried.

– Greg B Oct 18 '10 at 21:01 quotRem. – KennyTM Oct 18 '10 at 21:02 I've added an example to my code as I don't see how div and mod will help me walk over the digits of any arbitrary number. Could you expand on your thoughts please.

– Greg B Oct 18 '10 at 21:05 @Greg B this is a haskell source code that does the exact same thing your algorithm does, but using @supercooldave algorithm => pastie. Org/1231091 – Roman Gonzalez Oct 18 '10 at 21:37.

Digits :: Integer -> Int digits = map (read . (:)) . Show or you can return it into : digits :: Integer -> Int digits = map (read .

Return) . Show or, with Data.Char. DigitToInt: digits :: Integer -> Int digits = map digitToInt .

Show the same as Daniel's really, but pointless and uses Int, because a digit shouldn't really exceed maxBound :: Int.

Maybe (digits = map (read . Return) . Show)?

Or (read . Pure).. – Ed'ka Oct 19 '10 at 20:36 the digitToInt version is probably better anyway, and : was slightly more obvious to me. Eh, I'll edit it in.

I have no idea where pure is from, so. – sreservoir Oct 19 '10 at 22:00.

You can use digits = map (`mod` 10) . Reverse . TakeWhile (> 0) .

Iterate (`div` 10) or for reverse order rev_digits = map (`mod` 10) . TakeWhile (> 0) . Iterate (`div` 10) The iterate part generates an infinite list dividing the argument in every step by 10, so 12345 becomes 12345,1234,123,12,1,0,0... The takeWhile part takes only the interesting non-null part of the list.

Then we reverse (if we want to) and take the last digit of each number of the list. I used point-free style here, so you can imagine an invisible argument n on both sides of the "equation". However, if you want to write it that way, you have to substitute the top level .

By $: digits n = map(`mod` 10) $ reverse $ takeWhile (> 0) $ iterate (`div`10) n.

1 digits = map (read . (:)) . Show – sreservoir Oct 18 '10 at 22:47.

You could also just reuse digits from Hackage.

Textbook unfold import qualified Data. List as L digits = reverse . L.

Unfoldr (\x -> if x == 0 then Nothing else Just (mod x 10, div x 10)).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions