Skip to content

Instantly share code, notes, and snippets.

@sarath-soman
Created December 18, 2018 17:36
Show Gist options
  • Save sarath-soman/d1e85ae3ae0db92d15e696926cf25cab to your computer and use it in GitHub Desktop.
Save sarath-soman/d1e85ae3ae0db92d15e696926cf25cab to your computer and use it in GitHub Desktop.
sealed trait SKI
case object S extends SKI
case object K extends SKI
case object I extends SKI
case class APP(x: SKI, y: SKI) extends SKI
case class VAR(v: String) extends SKI
object SKI {
def eval(x: SKI): SKI = x match {
case APP(I, x) => eval(x)
case APP(APP(K, x), y) => eval(x)
case APP(APP(APP(S, x), y), z) => eval(APP(APP(x, z), APP(y, z)))
case APP(x, y) => {
val app = APP(eval(x), eval(y))
if(APP(x, y) == app) app
else eval(app)
}
case x => x
}
def main(args: Array[String]): Unit = {
println(eval(APP(I, VAR("x"))))
println(eval(APP(APP(K, APP(I, VAR("x"))), VAR("y"))))
println(eval(APP(APP(APP(S, I), I), VAR("x"))))
println(eval(APP(APP(I, I), VAR("x"))))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment