should function composition operators affect memory use?

I wrote the following program a while ago :-
import List
sumFacDigits 0 = 0 sumFacDigits n = product [1 .. mod n 10] + sumFacDigits (div n 10)
lenFacCycle n = length (nub (take 60 (iterate sumFacDigits n)))
main = do print ( sum [ 1 | n <- [1 .. ((10^6) - 1)], lenFacCycle n == 60 ] ) It compiles and runs fine.
To get rid of the brackets, I used the function composition operators . and $. (Incidentally why does nobody tell you that you can use . provided the last composition operator is $?) It now reads:-
import List
sumFacDigits 0 = 0 sumFacDigits n = product [1 .. mod n 10] + sumFacDigits n `div` 10
lenFacCycle n = length . nub . take 60 $ iterate sumFacDigits n
main = do print ( sum [ 1 | n <- [1 .. ((10^6) - 1)], lenFacCycle n == 60 ] ) This compiles. When I run it however, it exits with the stack exceeded message.
Why? Are the function composition operators more than syntactic sugar for the brackets?

On Sun, Nov 9, 2008 at 12:37 AM, Logesh Pillay
I wrote the following program a while ago :-
[snip]
This compiles. When I run it however, it exits with the stack exceeded message.
Why? Are the function composition operators more than syntactic sugar for the brackets?
Your problem is probably in this expression:
sumFacDigits n `div` 10
Becuase function application binds tightest, this is the same as:
div (sumFacDigitss n) 10
whereas your original was:
sumFacDigits (div n 10)
So your transformed program is not the same as your original. I hope that helps. -Antoine
participants (2)
-
Antoine Latter
-
Logesh Pillay