should function composition operators affect memory use?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

should function composition operators affect memory use?

Logesh Pillay-3
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?




Reply | Threaded
Open this post in threaded view
|

should function composition operators affect memory use?

Antoine Latter-2
On Sun, Nov 9, 2008 at 12:37 AM, Logesh Pillay <[hidden email]> wrote:
> 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