Created
July 20, 2015 09:08
-
-
Save shigemk2/d4f231b131b4aeed8fd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// スタックにすべて積んで、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