Skip to content

Instantly share code, notes, and snippets.

@stmtk1
Last active January 9, 2020 09:58
Show Gist options
  • Save stmtk1/64a01467c1a3bd89a8a5cb7a8b81539a to your computer and use it in GitHub Desktop.
Save stmtk1/64a01467c1a3bd89a8a5cb7a8b81539a to your computer and use it in GitHub Desktop.
import Control.Monad.State.Strict
import System.Random
main = do
print "hello world"
print $ evalState (genPrimes 100) [2]
isPrime :: Int -> [Int] -> Bool
isPrime n primes = all (\p -> mod n p /= 0) primes
getNextPrime :: [Int] -> Int
getNextPrime primes = head $ take 1 $ filter (\n -> isPrime n primes) $ [(head primes)..]
genPrime :: State [Int] Int
genPrime = state (\primes -> (\next_p -> (head primes, (next_p:primes))) $ getNextPrime primes)
genPrimes :: Int -> State [Int] [Int]
genPrimes n = replicateM n genPrime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment