Created
October 31, 2013 01:38
-
-
Save skiadas/7243203 to your computer and use it in GitHub Desktop.
A simple algorithm for constructing the sequence of prime numbers, in SML.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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