Haskell Lesson

2008-11-16T21:36:44Z

<asdftimo> hey roconnor, what do you think about this function for determining if a given number is prime:
<asdftimo> isprime n=not (elem 0 $ map(\x -> mod n x) $ takeWhile(\x -> x^2 <= n)(2:[3,5..])
<roconnor> asdftimo: that's not a bad start
<roconnor> asdftimo: it would be better if you test only prime numbers instead of 2 and the odd numbers
<asdftimo> im using isprime to generate
<asdftimo> primelist = [x | x <- 2:[3,5..], isprime x]
<asdftimo> yeah, im going to then use this list to find prime factors
<roconnor> how about primelist = 2:[x | x<-[3,5..], isprime x]
<asdftimo> i don't understand why that is an improvement
<roconnor> and then use takeWhile (\x -> x^2 <= n) primelist in your isprime
<asdftimo> roconnor: that's kind of strange, since they both point to each other, but i guess i'll go try that...
<roconnor> :)
<roconnor> you need to pull the 2: out in front in order for the recursive definition to get going.
<roconnor> kinda like a starter motor on an engine
<asdftimo> roconnor: that is fucking cool
<asdftimo> it works
<asdftimo> i love haskell
<asdftimo> it is going to take me another good 5 mins to comprehend this, but i can tell it is pretty amazing

#haskell

Tags

,

Responses


Russell O’Connor: contact me