Skip to content

Instantly share code, notes, and snippets.



Created May 31, 2018
What would you like to do?
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) + 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