Skip to content

Instantly share code, notes, and snippets.

@non
Created May 31, 2018 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non/2cc6f4ccb2ebcdd194a8f843cfe5f30e to your computer and use it in GitHub Desktop.
Save non/2cc6f4ccb2ebcdd194a8f843cfe5f30e to your computer and use it in GitHub Desktop.
package ack
import cats.Eval
import spire.math.SafeLong
object Ackermann {
import SafeLong.{zero, one}
// requires m >= 0 and n >= 0
def run(m: SafeLong, n: SafeLong): Eval[SafeLong] =
if (m.isZero) Eval.now(n + one)
else if (n.isZero) Eval.defer(run(m - one, one))
else Eval.defer(run(m, n - one)).flatMap(run(m - one, _))
def apply(m: SafeLong, n: SafeLong): SafeLong =
run(m, n).value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment