scala/ScalaBook/chapter-03/cont2.scala

import Cont.cont
sealed trait Cont[ρ,α] {
  def apply(f: α => ρ): ρ

  def map[β](f: α => β) =
    cont[ρ,β](k => apply(k compose f)) 

  def flatten[β](xc: α => Cont[ρ,β]) =
    cont[ρ,β](k => apply(xc(_)(k)))
}

object Cont {
  def cont[ρ,α](g: (α => ρ) => ρ) = new Cont[ρ,α] {
    def apply(f: α => ρ) = g(f)
  }

  def unit[ρ] = new AnyRef {
    def apply[α](x: α) = cont[ρ,α](k => k(x))
  }

  def callcc[ρ,α,β](g: (α => Cont[ρ,β]) => Cont[ρ,α]) =
    cont[ρ,α](k => g(l => cont(x => k(l)))(k))
}



import Cont._

  def square(n: Int) = n * n

 
  def squarec[R](n: Int) = unit[R](square(n))

  def squareE(n: Int) = squarec[Unit](n)

  val k = squareE(args(0).toInt)
  k(println)