Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pgujjula authored Apr 5, 2024
1 parent 7d1bcf8 commit 79f010a
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ SPDX-License-Identifier: BSD-3-Clause
[![CI](https://github.com/pgujjula/apply-merge/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/pgujjula/apply-merge/actions/workflows/ci.yml)
[![REUSE status](https://api.reuse.software/badge/github.com/pgujjula/apply-merge)](https://api.reuse.software/info/github.com/pgujjula/apply-merge)

⚠️ Under construction. No stability guarantees, even commit history may be
overwritten.

This library provides a function

```haskell
Expand All @@ -21,16 +24,6 @@ a sorted list of all `f x y`, for each `x` in `xs` and `y` in `ys`.
With `applyMerge`, we can implement a variety of complex algorithms succinctly.
For example, the Sieve of Erastosthenes[^1] to generate prime numbers:

[^1]: Note that this is really the Sieve of Erastosthenes, as defined in the
classic [The Genuine Sieve of Eratosthenes](https://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf).
Constrast this to other simple prime generation implementations, such as
```
primes = sieve [2..]
sieve (p : xs) = p : sieve [x | x <− xs, x ‘mod‘ p > 0]
```
which is actually trial division, and not a faithful implementation of the Sieve
of Erastosthenes.
```haskell
primes :: [Int]
primes = 2 : ([3..] `minus` composites) -- `minus` from data-ordlist
Expand All @@ -39,24 +32,37 @@ composites :: [Int]
composites = applyMerge (*) primes [2..]
```

3-smooth numbers ([Wikipedia](https://en.wikipedia.org/wiki/Smooth_number)):

```haskell
-- Square-free integers
squarefrees :: [Int]
squarefrees = [1..] `minus` applyMerge (*) (map (^2) primes) [1..]
smooth3 :: [Integer]
smooth3 = applyMerge (*) (iterate (*2) 1) (iterate (*3) 1)
```

Gaussian integers, ordered by norm ([Wikipedia](https://en.wikipedia.org/wiki/Gaussian_integer)):
```haskell
zs :: [Int]
zs = 0 : concatMap (\i -> [i, -i]) [1..]

-- Gaussian integers in the first quadrant, ordered by norm
gaussianInts :: [Complex Int]
gaussianInts = applyMergeBy (comparing (\a b -> a*a + b*b)) (:+) [1..] [1..]
gaussianInts = applyMergeBy (comparing (\a b -> a*a + b*b)) (:+) ints ints
```

Square-free integers ([Wikipedia](https://en.wikipedia.org/wiki/Square-free_integer)):

⚠️ Under construction. No stability guarantees, even commit history may be
overwritten.

See [ALGORITHM.md](docs/ALGORITHM.md) for a full description of the algorithm.
```haskell
squarefrees :: [Int]
squarefrees = [1..] `minus` applyMerge (*) (map (^2) primes) [1..]
```

See [ALGORITHM.md](docs/ALGORITHM.md) for a full exposition of the `applyMerge` function and its implementation.

## Licensing
This project licensed under BSD-3-Clause (except for `.gitignore`, which is
under CC0-1.0), and follows [REUSE](https://reuse.software) licensing
principles.

[^1]: Note that this is really the Sieve of Erastosthenes, as defined in the classic [The Genuine Sieve of Eratosthenes](https://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf). Constrast this to other simple prime generation implementations, such as <pre>
primes = sieve [2..]
sieve (p : xs) = p : sieve [x | x <- xs, x \`rem\` p > 0]</pre>
which are actually trial division and not faithful implementations of the Sieve of Erastosthenes.

0 comments on commit 79f010a

Please sign in to comment.