Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// スタックにすべて積んで、List("1", "+", "2")みたいな感じになったら、演算する
def ifn(str: String): Int = ifnPrime(str.split(" ").toList, Nil)
def ifnPrime(str: List[String], stack: List[String]): Int = (str, stack) match {
case ("+"::t, xs) => ifnPrime(t, xs :+ "+")
case ( n::t, zs) => {
val list = zs :+ n
list match {
case (x::op::y) if op == "+" => ifnPrime(t, List((x.toInt + y.head.toInt).toString))
case x => ifnPrime(t, x)
}
}
case _ => stack.head.toInt
}
println(ifn("1"))
println(ifn("1 + 2"))
println(ifn("1 + 2 + 3 + 4"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment