Skip to content

Instantly share code, notes, and snippets.

@papamitra
Created September 11, 2010 10:09
Show Gist options
  • Save papamitra/575055 to your computer and use it in GitHub Desktop.
Save papamitra/575055 to your computer and use it in GitHub Desktop.
// Scalaの限定継続で四天王問題解いてみた
// scalac -P:continuations:enable Amb.scala
import scala.util.continuations._
object Amb{
def amb(lst:Int*):Int @ cpsParam[Unit,Unit] = shift{k:(Int=>Unit) => lst.foreach(k)}
def require(p:Boolean):Unit @ cpsParam[Unit,Unit] = shift{k:(Unit=>Unit) => if(p)k()}
def distinct(lst:Int*)= {
def proc(l:List[Int]):Boolean = l match{
case Nil => true
case x::xs => (xs.indexOf(x)<0) && proc(xs)
}
proc(lst.toList)
}
def main(args:Array[String]){
reset{
val a,b,c,d = amb(1,2,3,4)
// 同順が無いことを仮定
require(distinct(a,b,c,d))
// A「Dがやられたようだな…」B「ククク…奴は我ら四天王の中でも最弱…」
require(d==4)
// C「私はBよりも弱い…」
require(b < c)
// A「そして私は最強ではない…」
require(a != 1)
// B「四天王の中に私よりも弱いものが最低でも二人いる…」
require(b==1 || b==2)
// C「私はAよりも強い…」
require(c < a)
// ※以上の条件から四天王を強い順に並べよ(5点)
println(("abcd" zip List(a,b,c,d)).toList.sortWith(_._2 < _._2))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment