Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def pn(str: String): Double = pnPrime(str.split(" ").toList)._1
// 一意型パターン
def pnPrime(src: List[String]): (Double, List[String]) = src match {
case "+"::xs => {
val (a, n1) = pnPrime(xs)
val (b, n2) = pnPrime(n1)
(a.toInt + b, n2)
}
case "-"::xs => {
val (a, n1) = pnPrime(xs)
val (b, n2) = pnPrime(n1)
(a.toInt - b, n2)
}
case "*"::xs => {
val (a, n1) = pnPrime(xs)
val (b, n2) = pnPrime(n1)
(a.toInt * b, n2)
}
case "/"::xs => {
val (a, n1) = pnPrime(xs)
val (b, n2) = pnPrime(n1)
(a.toInt / b, n2)
}
case "%"::xs => {
val (a, n1) = pnPrime(xs)
val (b, n2) = pnPrime(n1)
(a.toInt % b, n2)
}
case _ => (src.head.toInt, src.tail)
}
println(pn("1"))
println(pn("+ 1 2"))
println(pn("+ 1 + 2 3"))
println(pn("+ + 1 2 3"))
println(pn("+ + 1 2 + 3 4"))
println(pn("- 4 2"))
println(pn("* 4 2"))
println(pn("/ 4 2"))
println(pn("% 4 2"))
println(pn("* + 4 2 2"))
println(pn("* + 4 2 - 4 2"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment