[Haskell-cafe] N00b question

Jim Burton jim at sdf-eu.org
Mon Jul 2 11:10:33 EDT 2007



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.
> 
> 

-- 
View this message in context: http://www.nabble.com/N00b-question-tf4010616.html#a11395620
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list