haskell list comprehension optimization -


i have piece of code written project euler:

primes :: [integer] primes = filter isprime [2..]     isprime n = not $ (\x -> n `mod` x == 0) [2..n-1]  ans = max [x | x <- primes, 600851475143 `mod` x == 0] 

of course didn't wait terminate. i'm wondering ever return? guess depends on whether haskell knows stop @ 600851475143?

no, never return. haskell has non-strict semantics, compliant implementation doesn't need evaluate parts of expression unused; not theorem prover. runtime system not attempt prove there can't larger number in list. instead, when expression evaluated max will have @ every single item in list determine maximum value. going problem, because per said above, list can never determined done (because source list infinite, there's more elements potentially included in list expression).

what need here takewhile followed filter before max.

also, don't need point out primes method incredibly inefficient , take really long time generate numbers high seem need problem.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -