Skip to content

Instantly share code, notes, and snippets.

@skiadas
Created October 31, 2013 01:38
Show Gist options
  • Save skiadas/7243203 to your computer and use it in GitHub Desktop.
Save skiadas/7243203 to your computer and use it in GitHub Desktop.
A simple algorithm for constructing the sequence of prime numbers, in SML.
fun divisible x xs =
case xs of
[] => false
| y::xs' => x mod y = 0 orelse divisible x xs'
fun primes lst =
let fun acc(x, rest) =
if divisible x rest then rest else x::rest
in rev (foldl acc [] lst)
end
fun from2 n =
let fun aux (k, lst) = if k < 2 then lst else aux (k-1, k::lst)
in aux (n, [])
end
primes (from2 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment