Created
May 31, 2018 18:06
-
-
Save non/2cc6f4ccb2ebcdd194a8f843cfe5f30e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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