Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. object ISort {
  2. def sort(l: List[Int]): List[Int] = {
  3. l match {
  4. case Nil => Nil
  5. case head :: tail => insert(head, sort(tail))
  6. }
  7. }
  8.  
  9. def insert(x: Int, l: List[Int]): List[Int] = {
  10. l match {
  11. case Nil => List(x)
  12. case head :: tail =>
  13. if (x <= head) {
  14. x :: l
  15. } else {
  16. head :: insert(x, tail)
  17. }
  18. }
  19. }
  20. }
  21.  
  22. val MagicNumber = 50
  23.  
  24. def typedMatch(x: Any) {
  25. x match {
  26. case s: String => println("Text: " + s)
  27. case c: Char => println("Character: " + c)
  28. case _: Int => println("A number, any number!")
  29. case _ => println("Anything, anything at all!")
  30. }
  31. }
  32.  
  33. def intMatch(i: Int, special: Int) {
  34. i match {
  35. case 9001 => println("A literal value over nine thounsand!")
  36. case MagicNumber => println("A magic number!")
  37. case 5 | 3 => println("Either five or three!")
  38. case `special` => println("A special number!")
  39. }
  40. }
  41.  
  42. def conditionalMatch(s: String) {
  43. s match {
  44. case x if x.length > 10 => println("A long string")
  45. case x if x.startsWith("asdf") => println("""String starting with "asdf"!""")
  46. }
  47. }
  48.  
  49. def listMatch(l: List[Int]) {
  50. l match {
  51. case Nil => println("An empty list!")
  52. case head :: Nil => println("A list with one element (%d)" format head)
  53. case head :: tail => println("A longer list!")
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement