amuletml / amulet

An ML-like functional programming language
https://amulet.works/
BSD 3-Clause "New" or "Revised" License
328 stars 16 forks source link

add a library of stream functions #261

Closed plt-amy closed 4 years ago

plt-amy commented 4 years ago
let primes =
  let open Seq in
  let rec sieve p =
    let (p, xs) = from_option (lazy (error "wasn't infinite")) (uncons p)
    p :> lazy (xs |> filter ((> 0) % flip mod p) |> sieve)
  sieve (arithmetic_seq 2 3)

let factor i =
  let rec go acc primes i =
    match i, Seq.uncons primes with
    | 1, _  -> acc
    | p, None -> p :: acc
    | i, Some (p, _) when i `mod` p == 0 ->
        go (p :: acc) primes (i // p)
    | i, Some (_, ps) -> go acc ps i

  go [] (primes |> Seq.take_while (fun p -> p <= int_of_float (i / 2))) i