« Return to Thread: N00b question

Re: N00b question

by Jim Burton :: Rate this Message:

Reply to Author | View in Thread

poop wrote:
So I'm working my way thorough haskell, doing some programming problems, and I have this one so far:
Hi, I haven't spotted the problem in your code but there's an alternative solution to Euler Problem 14 on the wiki: http://www.haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_14 -- it may be helpful as a comparison?
Regards,
p14next n = if (mod n 2 == 0) then (div n 2) else (3*n + 1)

p14seqlen n = f' 1 n where
        f' accum n
                | n == 1 = accum
                | otherwise = f' (accum + 1) (p14next n)

sndmax a b = if snd a > snd b then a else b

p14 n = fst (f' (0,0) n) where
        f' maxTuple n
                | n == 0 = maxTuple
                | otherwise = f' (sndmax maxTuple (n, p14seqlen n)) (n-1)

the goal is to be able to run:
p14 999999

and not get a stack overflow message.  I read what was available on tail recursion and I think I have it working with tail recursion now, at least it gives me a stack overflow message really quickly compared to before :)

I have not been able to find any documentation on seq and ($!) which was also mentioned as a way to get rid of stack overflow messages so if those are what is required an explanation of those would be most helpful.

 « Return to Thread: N00b question