Created
July 20, 2015 09:06
-
-
Save shigemk2/bebe5df023cf6e625d43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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[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