Possible Duplicate:
Split a number into its digits with Haskell
how can i convert integer to integer list Exmple: input: 1234 output:[1,2,3,4] any idee about this problem ?
Possible Duplicate:
Split a number into its digits with Haskell
how can i convert integer to integer list Exmple: input: 1234 output:[1,2,3,4] any idee about this problem ?
This sounds like homework. Here's a general algorithm that you should be able to apply in Haskell:
Good luck.
Using integer arithmetic:
digits' 0 = []
digits' n = n `rem` 10 : digits (n `quot` 10)
digits n = reverse (digits' n)
Solution:
digs 0 = [] digs x = digs (x `div` 10) ++ [x `mod` 10]
Source: link
jleedev:
I did something similar with divMod. I did not know that quotRem existed!
import Data.List ( unfoldr )
listify :: Integer -> [Integer]
listify = reverse . unfoldr f
where
f i = case divMod i 10 of
(0, 0) -> Nothing
(w, r) -> Just (r, w)
What about this quite simple solution?
import Data.Char (digitToInt)
int2intList :: Integral i => i -> [Int]
int2intList s = map digitToInt $ show s
main = print $ int2intList 12351234999123123123
gives [1,2,3,5,1,2,3,4,9,9,9,1,2,3,1,2,3,1,2,3]
This one is possible and a bit more universal, too:
int2intList :: (Read i, Integral i) => i -> [i]
int2intList s = map (read.(:[])) $ show s
main = print $ int2intList 12351234999123123123