Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just because I can't resist writing little math things in Haskell:

    primes = 2 : filter isPrime [3..]

    isPrime n =
      let divisors = takeWhile (\d -> d * d <= n) primes
      in not $ any (\d -> n `mod` d == 0) divisors

    strongPrimes =
      let primeTriples = zip3 primes (drop 1 primes) (drop 2 primes)
      in [b | (a, b, c) <- primeTriples, b*2 > a + c]


Proof that you can write something elegant in Haskell and use comprehensible variable names.


I like the zip3, I should have thought of that. I think I prefer the mapMaybe, but then I avoid list comprehensions.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: