Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Fibonacci and Sieve of Erasthosthenes examples in appropriate places #31

Open
klaeufer opened this issue Nov 26, 2023 · 0 comments
Open
Assignees

Comments

@klaeufer
Copy link
Member

klaeufer commented Nov 26, 2023

Start with imperative, then naive, inefficient recursive version, then introduce accumulator.

scala> @scala.annotation.tailrec def fib(k: Int, i: Int = 0, m: BigInt = 0, n: BigInt = 1): BigInt =
     |   if (i >= k) m else fib(k, i + 1, n, m + n)

Efficient but doesn't memoize!

and/or

def facFrom(n: Int, r: BigInt): LazyList[BigInt] = n #:: facFrom(n + 1, n * r)

def fibFrom(a: BigInt, b: BigInt): LazyList[BigInt] = a #:: fibFrom(b, a + b)

Also implement nonrecursively using iterate!

val fib = LazyList.iterate((BigInt(0), BigInt(1)))((m, n) => (n, m + n)).map(_._1)

Do something similar for the sieve (corecursion)!

scala> def era(ns: Iterator[Int]): (Int, Iterator[Int]) =
     |   val p = ns.next
     |   (p, ns.filter(_ % p != 0))

val primes = Iterator.iterate(0, Iterator.from(2))((p, ns) => era(ns)).drop(1)

primes.next

TODO express as LazyList

Seq.unfold is the equivalent to Iterator.iterate for building finite structures; both can be considered anamorphisms.

Then add F-algebraic versions based on Droste examples and reference below.

Reference: https://bartoszmilewski.com/2017/02/28/f-algebras/

@klaeufer klaeufer self-assigned this Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant