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
| import Data.List.Split | |
| import Control.Monad.State | |
| -- 状態は引数としては明示的に書かない | |
| pn str = fst $ pnPrime $ splitOn " " str | |
| pnPrime ("+":s0) = | |
| let (a, s1) = pnPrime s0 | |
| (b, s2) = pnPrime s1 | |
| in (a + b, s2) | |
| pnPrime (x:xs) = ((read x:: Int), xs) |
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
| import Data.List.Split | |
| pn str = fst $ pnPrime $ splitOn " " str | |
| pnPrime ("+":xs) = | |
| let (a, n1) = pnPrime(xs) | |
| (b, n2) = pnPrime(n1) | |
| in (a + b, n2) | |
| pnPrime src = ((read $ head src :: Int), tail src) | |
| main = do |
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
| def pn(str: String): Int = pnPrime(str.split(" ").toList)._1 | |
| // 一意型パターン | |
| def pnPrime(src: List[String]): (Int, List[String]) = src match { | |
| case "+"::xs => { | |
| val (a, n1) = pnPrime(xs) | |
| val (b, n2) = pnPrime(n1) | |
| (a.toInt + b, n2) | |
| } | |
| case _ => (src.head.toInt, src.tail) |
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
| def pn(str: String): Int = pnPrime(str.split(" ").toList)._1 | |
| def pnPrime(src: List[String]): (Int, List[String]) = src match { | |
| case "+"::x::ys => { | |
| val a = x.toInt | |
| val (b, next) = pnPrime(ys) | |
| (a + b, next) | |
| } | |
| case _ => (src.head.toInt, src.tail) | |
| } |
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
| def pn(str: String): Int = pnPrime(str.split(" ").toList) | |
| def pnPrime(list: List[String]): Int = list match { | |
| case "+"::x::ys => x.toInt + pnPrime(ys) | |
| case _ => list.head.toInt | |
| } | |
| println(pn("1")) | |
| println(pn("+ 1 2")) | |
| println(pn("+ 1 + 2 3")) |
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
| def pnPrime(str: String): Double = str.split(" ").toList match { | |
| case (x::y::zs) if x == "+" => y.toDouble + zs.head.toDouble | |
| } | |
| println(pnPrime("+ 1 2")) |
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
| def rpn(str: String): Int = rpnPrime(str.split(' ').toList, Nil) | |
| def rpnPrime(str: List[String], stack: List[Int]): Int = (str, stack) match { | |
| case ("+"::t, y::x::zs) => { | |
| println(s"str: $str, stack: $stack") | |
| rpnPrime(t, (x + y) :: zs) | |
| } | |
| case ("-"::t, y::x::zs) => { | |
| println(s"str: $str, stack: $stack") | |
| rpnPrime(t, (x - y) :: zs) |
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
| def solveRPN(input: String): Double = { | |
| input.split(" ").toList.foldLeft(List[Double]()){(out, in) => | |
| in match { | |
| case "+" => out.dropRight(2) :+ (out.dropRight(1).last + out.last) | |
| case "-" => out.dropRight(2) :+ (out.dropRight(1).last - out.last) | |
| case "*" => out.dropRight(2) :+ (out.dropRight(1).last * out.last) | |
| case "/" => out.dropRight(2) :+ (out.dropRight(1).last / out.last) | |
| case "%" => out.dropRight(2) :+ (out.dropRight(1).last % out.last) | |
| case _ => out :+ in.toDouble | |
| } |
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
| // def rpnPrime(str: String): String = str match { | |
| // case str if str(4) == '+' => (str(0).asDigit + str(2).asDigit).toString | |
| // case str if str(4) == '-' => (str(0).asDigit - str(2).asDigit).toString | |
| // case str if str(4) == '*' => (str(0).asDigit * str(2).asDigit).toString | |
| // case str if str(4) == '/' => (str(0).asDigit / str(2).asDigit).toString | |
| // case str if str(4) == '%' => (str(0).asDigit % str(2).asDigit).toString | |
| // case _ => { | |
| // "" | |
| // } | |
| // } |
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
| def sieve(xs: List[Int]): List[Int] = xs match { | |
| case xs if xs.isEmpty => Nil | |
| case (x::xs) => x :: sieve(xs.filter(_ % x != 0)) | |
| } | |
| println(sieve(Range(2, 100).toList)) |