[Haskell-cafe] Folding Integrals
Reinier Lamers
reinier.lamers at phil.uu.nl
Wed Dec 12 05:21:57 EST 2007
Mattias Bengtsson wrote:
>I found myself writing this for an Euler-problem:
>
>
>
>>digits :: Int -> [Int]
>>digits i | i < 10 = [i]
>> | otherwise = i `mod` 10 : digits ( i `div` 10 )
>>
>>
>
>And i realised it was quite some time ago (before this function) i had
>actually written any explicitly recursive function.
>
Back in my Introduction to Functional Programming course, Daan Leijen
demonstrated how to print integers in Haskell using function
composition. Something along the lines of:
printint :: Int -> [Char]
printint = map chr . map (+0x30) . reverse . map (`mod` 10) . takeWhile
(>0) . iterate (`div`10)
You can easily translate a number to a list of digits without explicit
recursion.
Regards,
Reinier
More information about the Haskell-Cafe
mailing list