Last active
November 19, 2019 02:35
-
-
Save VictoryWangCN/9af950a5e6a7c41c0e830c3f76160fca 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
object L { | |
def main(args: Array[String]): Unit = { | |
val oneToTenList = (1 to 10).toList | |
val oneList = List(1) | |
assert("none" == checker(11)(oneToTenList)) | |
assert("end of 10" == checker(10)(oneToTenList)) | |
assert("contains 9" == checker(9)(oneToTenList)) | |
assert("start with 1" == checker(1)(oneToTenList)) | |
assert("none" == checker(0)(oneList)) | |
assert("start or end of 1" == checker(1)(oneList)) | |
assert("none" == checker(1)(Nil)) | |
} | |
def checker(item: Int): List[Int] => String = { | |
case Nil => "none" | |
case h :: Nil if h equals item => s"start or end of $item" | |
case h :: _ if h equals item => s"start with $item" | |
case _ :: rest => contain(item)(rest) | |
} | |
def contain(item: Int): List[Int] => String = { | |
case Nil => "none" | |
case h :: Nil if h.equals(item) => s"end of $item" | |
case h :: rest => if (h.equals(item)) s"contains $item" else contain(item)(rest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment