Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell program crashing - infinite recursion? wrong where statement?

I need to calculate the product of all factorials from 1..n. When I call this function double_factorial (with at least 2 or 3 as args), it seems to be called for a moment, but nothing happens, and after a few seconds the GHCi just closes. What is wrong? Is there some infinite recursion that I can't see? Here is my code :

double_factorial :: Integer->Integer
double_factorial n 
    | n<0 = error "negative number is given"
    | n==0 = 1
    | otherwise = (factorial n)*(double_factorial n-1)
    where
    factorial :: Integer->Integer
    factorial n 
        | n == 0  = 1
        | otherwise = n*(factorial n-1)
like image 282
Saskia Avatar asked Nov 30 '22 01:11

Saskia


1 Answers

(double_factorial n-1) means ((double_factorial n) - 1) so yes, it's an infinite recursion problem.

like image 78
Marcin Łoś Avatar answered Dec 01 '22 16:12

Marcin Łoś