
12 Dec
2007
12 Dec
'07
5:21 a.m.
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