Haskell - How to split a number into a list for further processing?

I have Int that I want to divide into it separate numbers, which ideally will be contained in a list, which I can then process further. So I would like something like this:

split 245 --will then get an list containing [2,4,5] 

Anyone familiar with this feature?

+7
split numbers haskell
source share
5 answers
 import Data.Char map digitToInt $ show 245 
+22
source share

will an example work for you? http://snippets.dzone.com/posts/show/5961

 convRadix :: (Integral b) => b -> b -> [b] convRadix n = unfoldr (\b -> if b == 0 then Nothing else Just (b `mod` n, b `div` n)) 

Example:

 > convRadix 10 1234 [4, 3, 2, 1] > convRadix 10 0 [] > convRadix 10 (-1) [9,9,...] (infinite) to convert haskell radix by mokehehe on Thu Aug 21 08:11:39 -0400 2008 
+4
source share
 digits :: Int -> [Int] digits 0 = [] digits n = digits k ++ [r] where k = div n 10; r = mod n 10 
+2
source share
 digits :: (Integral a) => a -> [a] digits = flip digits' [] . abs digits' :: (Integral a) => a -> ([a] -> [a]) digits' n = if q == 0 then (r :) else (digits q ++) . (r :) where (q, r) = n `divMod` 10 

 digits 1234 == [1, 2, 3, 4] digits (-1234) == [1, 2, 3, 4] 
+1
source share
 digits = reverse . map (`mod` 10) . takeWhile (> 0) . iterate (`div` 10) . abs 
+1
source share

All Articles