[Haskell-beginners] should function composition operators affect memory use?

Logesh Pillay lpillay at webafrica.org.za
Sun Nov 9 01:37:29 EST 2008


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?






More information about the Beginners mailing list