Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created July 20, 2015 09:06
Show Gist options
  • Select an option

  • Save shigemk2/bebe5df023cf6e625d43 to your computer and use it in GitHub Desktop.

Select an option

Save shigemk2/bebe5df023cf6e625d43 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[Int]): Int = {
println(s"str: $str stack: $stack")
(str, stack) match {
case ("+"::t, x::xs) => x + ifnPrime(t, Nil)
case ( n::t, _ ) => ifnPrime(t, n.toInt::stack)
case _ => stack.head
}
}
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