fork download
  1. object Main{
  2. def doSomething(l : MyList, i : Int):MyList = {
  3. l match {
  4. case Nil() => Nil()
  5. case Cons(e,tail) if e > i => Cons(e,doSomething(tail,i))
  6. case Cons(e,tail) => doSomething(tail,i)
  7. }
  8. }
  9.  
  10. def doIt(l : MyList) : Int = {
  11. l match {
  12. case Nil() =>
  13. print(0)
  14. 0
  15. case Cons(v,t) =>
  16. print(v + doIt(t))
  17. v
  18. }
  19. }
  20. def doIt(l : MyList, i : Int) : MyList = {
  21. (l,i) match {
  22. case (Nil(),_) => Nil()
  23. case (_,0) => Nil()
  24. case (Cons(e,t),j) => Cons(e,doIt(t,j-1))
  25. }
  26. }
  27. def main(args: Array[String]) {
  28. val ll = Cons(1,Cons(2,Cons(3,Nil())))
  29. val k = doIt(ll)
  30. }
  31. }
  32.  
  33. case class Nil extends MyList
  34.  
  35. case class Cons(cur : Int , follow : MyList) extends MyList
  36.  
  37. object MyList {
  38. def push(n : Int, l : MyList):MyList = {
  39. Cons(n,l)
  40. }
  41. def tail(l : MyList) : MyList = {
  42. l match{
  43. case Nil() => Nil()
  44. case Cons(c,f) => f
  45. }
  46. }
  47. def pop(l : MyList) : (Int,MyList) = {
  48. l match{
  49. case Nil() => throw new IllegalArgumentException("wrong parameter")
  50. case Cons(c,f) => (c,f)
  51. }
  52. }
  53. }
Success #stdin #stdout 0.07s 211584KB
stdin
Standard input is empty
stdout
0353