packages feed

egison-4.0.0: sample/primes.egi

--
--
-- Pattern-matching against sequence of natural numbers
--
--

-- Extract all twin primes from the infinite list of prime numbers with pattern-matching!
twinPrimes :=
  matchAll primes as list integer with
    | _ ++ $p :: #(p + 2) :: _ -> (p, p + 2)

-- Enumerate the first 10 twin primes
assertEqual "first 10 twin prime"
  (take 10 twinPrimes)
  [ (3, 5)
  , (5, 7)
  , (11, 13)
  , (17, 19)
  , (29, 31)
  , (41, 43)
  , (59, 61)
  , (71, 73)
  , (101, 103)
  , (107, 109) ]

-- Extract all prime-triplets from the infinite list of prime numbers with pattern-matching!
primeTriplets :=
  matchAll primes as list integer with
    | _ ++ $p :: ($m & (#(p + 2) | #(p + 4))) :: #(p + 6) :: _ -> (p, m, p + 6)

-- Enumerate the first 10 prime triplets
assertEqual "first 10 prime triplets"
  (take 10 primeTriplets)
  [ (5, 7, 11)
  , (7, 11, 13)
  , (11, 13, 17)
  , (13, 17, 19)
  , (17, 19, 23)
  , (37, 41, 43)
  , (41, 43, 47)
  , (67, 71, 73)
  , (97, 101, 103)
  , (101, 103, 107) ]