Skip to content

Instantly share code, notes, and snippets.

@kthompson
Created March 22, 2021 18:30
Show Gist options
  • Save kthompson/94c0511efcb4c72dec14d12b341f396f to your computer and use it in GitHub Desktop.
Save kthompson/94c0511efcb4c72dec14d12b341f396f to your computer and use it in GitHub Desktop.
type CBool[A] = (=> A, => A) => A
type CNumber[A] = (A => A, A) => A
def ctrue[A](t: => A, f: => A): A = t
def cfalse[A](t: => A, f: => A): A = f
def cond[A](c: CBool[A], x: => A, y: => A): A = c(x, y)
def c0[A](f: A => A, x: A): A = x
def c1[A](f: A => A, x: A): A = f(c0(f, x))
def c2[A](f: A => A, x: A): A = f(c1(f, x))
def c3[A](f: A => A, x: A): A = f(c2(f, x))
def succ[A](n: CNumber[A], f: A => A, x: A): A = f(n(f, x))
def iszero[A](n: CNumber[CBool[A]]): CBool[A] = n(a => cfalse[A], ctrue[A])
val hi: String = cond(ctrue[String], "yes", "no")
val hi2: String = cond(cfalse[String], "yes", "no")
println(cond(iszero[String](c0), "iszero", "error"))
println(cond(iszero[String](c1), "error", "not zero"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment