Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an Int that i want to split into it's individual numbers which ideally would 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]

Is anyone familiar with such a function?

like image 494
RCIX Avatar asked Jan 14 '10 04:01

RCIX


2 Answers

import Data.Char

map digitToInt $ show 245
like image 197
Michał Marczyk Avatar answered Nov 05 '22 12:11

Michał Marczyk


would the example here 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
like image 29
John Boker Avatar answered Nov 05 '22 12:11

John Boker