This is a simple practice problem. The task is to write a function getLR
that receives an Integral
, n
, and returns a list representation (LR) of n
. For example: 73847 -> [7,3,8,4,7]
. Negative numbers return the LR of the absolute value, for example: -837 -> [8,3,7]
. In the following code I use three helper functions getAbs
, getI
and getList
:
getLR :: (Integral a) => a -> [a]
getLR n = if n==0 then [0] else getAbs n
getAbs :: (Integral a) => a -> [a]
getAbs n = if n>=0 then getI n n 0 else getI (-n) (-n) 0
-- Computes the length of an Integer and stores it in i, for example 321 has length 3, 9999 has length 4.
getI :: (Integral a) => a -> a -> a -> [a]
getI n m i = if m==0 then getList n [] (i-1) else getI n (m`div`10) (i+1)
-- Constructs the LR recursively.
getList :: (Integral a) => a -> [a] -> a -> [a]
getList n ns i = if n==0 then ns else getList (n-10^i*(n`div`(10^i))) (ns++[n`div`(10^i)]) (i-1)
The code works correctly. My specific concerns are readability and simplicity of the code. If you have any suggestions how to make the code more readable or more simple I'd be happy to receive your feedback. If you find any problems besides readability and simplicity: don't hesitate to point them out as well.