Why is this Haskell expression so slow?

后端 未结 3 1422
自闭症患者
自闭症患者 2020-12-31 16:46

I\'m working on Project Euler Problem 14. Here\'s my solution.

import Data.List

collatzLength :: Int->Int
collatzLength 1 = 1
collatzLength n | odd n = 1         


        
3条回答
  •  长情又很酷
    2020-12-31 17:32

    For being able to find a maximum of a list, the whole list needs to be evaluated.

    So it will calculate collatzLength from 1 to 1000000 and collatzLength is recursive. The worst thing is, that your definition of collatzLength is even not tail-recursive.

提交回复
热议问题