[Haskell-beginners] Haskell Generic Function Question

Thomas Friedrich info at suud.de
Thu May 28 13:05:12 EDT 2009


Hi William,

I would also strongly success not to make the detour using read and 
show.  Keep things simple.  Here is my suggestion:

toDigit x = case f x of
              (0,b) -> [b]
              (a,b) -> toDigit a    ++ [b]

f = \x -> (x `quot` 10, x `mod` 10)

Best,
Thomas




William Gilbert wrote:
> I am trying to write a function that will covert either an integer or
> an int into a list containing its digits.
>
> ex. toIntegralList 123 -> [1,2,3]
>
> I have written the following definition that tries to use read to
> generically cast a string value to an Integral type that is the same
> as the Integral passed in:
>
> toIntegralList :: (Integral a) => a -> [a]
> toIntegralList x = map (\c -> read [c] :: a) (show x)
>
> I understand it would be very simple to just create two functions, one
> that converts an Int and one that converts an Integer, however I was
> wondering if there were any way to accomplish what I am trying to do
> here.
>
> Thanks In Advance,
> Bryan
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>   



More information about the Beginners mailing list