Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created July 20, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/d4f231b131b4aeed8fd1 to your computer and use it in GitHub Desktop.
Save shigemk2/d4f231b131b4aeed8fd1 to your computer and use it in GitHub Desktop.
// スタックにすべて積んで、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